jquery事件不工作 - 表单字段

时间:2021-03-05 23:54:05

I've a change event in a jquery script which fails to fire but I cannot understand what is wrong/missing (there is also a datepicker & timepicker which is working ok). Any advice - greatly welcomed. Thanks.

我在jquery脚本中有一个更改事件,但是我无法理解错误/缺失(还有一个正在运行的datepicker和timepicker)。任何建议 - 非常欢迎。谢谢。

e.g.

jquery

$(document).ready(function() {
  
    $("#to_place").change(function () {         <==
        alert("Hello! I am an alert box!!");    <== not working?
    });
    
    $( "#date" ).datepicker({
        inline: true
    });

    $('.bfh-timepicker').timepicki({
                    show_meridian:false,
                    min_hour_value:0,
                    max_hour_value:23,
                    step_size_minutes:5,
                    overflow_minutes:true,
                    increase_direction:'up',
                    disable_keyboard_mobile: false});   
});

**HTML**
                <div class="form-group col-lg-3 col-md-3 col-sm-3 col-xs-12">
                    <label for="to">To</label>
                    <input placeholder="to" class="form-control" name="to_place" type="text">
                    
                </div>

5 个解决方案

#1


to_place is name of the input field so try this:

to_place是输入字段的名称,所以试试这个:

 $("input[name='to_place']").change(function () {
        alert("Hello! I am an alert box!!");
    });

#2


Your change event is binding to an element with the ID 'to_place' so add an ID to the input field. e.g.

您的更改事件绑定到ID为“to_place”的元素,因此请在输入字段中添加ID。例如

...
<input placeholder="to" class="form-control" id="to_place" name="to_place" type="text">
...

#3


$("#to_place") part of the code is searching for an element with id="to_place" that is not found on your snippet. Adding this to your input element should be enough to trigger the change event.

$(“#to_place”)部分代码正在搜索您的代码段中找不到的id =“to_place”的元素。将此添加到input元素应该足以触发change事件。

#4


Your jQuery selector "#to_place" is an ID selector, but you didn't set the ID for the text box. You can use "input[name=to_place]" instead of that. Or you can just add the ID attribute to the input field.

您的jQuery选择器“#to_place”是一个ID选择器,但您没有为文本框设置ID。您可以使用“input [name = to_place]”而不是它。或者您只需将ID属性添加到输入字段即可。

#5


Please check the below answer, it will work fine:

请检查以下答案,它将正常工作:

$(document).ready(function() {
    $("#to_place").change(function () {    
        alert("Hello! I am an alert box from change function!!");
    });   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="form-group col-lg-3 col-md-3 col-sm-3 col-xs-12">
        <label for="to">To</label>
        <input placeholder="to" class="form-control" name="to_place" type="text" id="to_place" />
 </div>

#1


to_place is name of the input field so try this:

to_place是输入字段的名称,所以试试这个:

 $("input[name='to_place']").change(function () {
        alert("Hello! I am an alert box!!");
    });

#2


Your change event is binding to an element with the ID 'to_place' so add an ID to the input field. e.g.

您的更改事件绑定到ID为“to_place”的元素,因此请在输入字段中添加ID。例如

...
<input placeholder="to" class="form-control" id="to_place" name="to_place" type="text">
...

#3


$("#to_place") part of the code is searching for an element with id="to_place" that is not found on your snippet. Adding this to your input element should be enough to trigger the change event.

$(“#to_place”)部分代码正在搜索您的代码段中找不到的id =“to_place”的元素。将此添加到input元素应该足以触发change事件。

#4


Your jQuery selector "#to_place" is an ID selector, but you didn't set the ID for the text box. You can use "input[name=to_place]" instead of that. Or you can just add the ID attribute to the input field.

您的jQuery选择器“#to_place”是一个ID选择器,但您没有为文本框设置ID。您可以使用“input [name = to_place]”而不是它。或者您只需将ID属性添加到输入字段即可。

#5


Please check the below answer, it will work fine:

请检查以下答案,它将正常工作:

$(document).ready(function() {
    $("#to_place").change(function () {    
        alert("Hello! I am an alert box from change function!!");
    });   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="form-group col-lg-3 col-md-3 col-sm-3 col-xs-12">
        <label for="to">To</label>
        <input placeholder="to" class="form-control" name="to_place" type="text" id="to_place" />
 </div>