如何使用JQuery绑定文本框的onchange事件?

时间:2022-11-07 13:58:04

How can I bind an OnChange event of a TextBox to function using jQuery?

如何使用jQuery将文本框的OnChange事件绑定到函数中?

Am trying this and its failing:

我正在尝试这个方法,但失败了:

$(document).ready(function() {
    $("input#tags").bind("change",autoFill);
});

function autoFill() {
    $("#tags").autocomplete("/taglookup/", {
        width: 320,
        max: 4,
        highlight: false,
        multiple: true,
        multipleSeparator:",",
        scroll: true,
        scrollHeight: 300,
        delay: 10
    });
}

NB: I want to implement a Tag TextBox like the one for * which detects OnChange events and calls AJAX functions when a user types in.

NB:我想实现一个像*那样的标记文本框,它可以检测到OnChange事件,并在用户输入时调用AJAX函数。

7 个解决方案

#1


31  

You're looking for keydown/press/up

你正在寻找keydown /新闻/

$("#inputID").keydown(function(event) {
    alert(event.keyCode);
});

or using bind $("#inputID").bind('onkeydown', ...

或者使用绑定美元(“# inputID”)。绑定(“onkeydown”,…

http://docs.jquery.com/Events

http://docs.jquery.com/Events

#2


18  

You must change the event name from "change" to "onchange":

您必须将事件名称从“更改”更改为“onchange”:

$(document).ready(function(){
        $("input#tags").bind("onchange", autoFill);
          });

or use the shortcut binder method change:

或使用快捷绑定方法更改:

$(document).ready(function(){
        $("input#tags").change(autoFill);
          });

Note that the onchange event usually fires when the user leave the input, so for auto-complete you better use the keydown event.

注意,onchange事件通常在用户离开输入时触发,因此为了自动完成,最好使用keydown事件。

#3


7  

Combination of keyup and change is not necessarily enough (browser's autocomplete and paste using mouse also changes the contents of a text box, but doesn't fire either of these events):

按键和更改的组合不一定足够(浏览器的自动完成和使用鼠标粘贴也会更改文本框的内容,但不会触发这两个事件):

jquery change not working incase of dynamic value change

jquery在动态值更改的情况下不工作

#4


1  

Here's a clue why an onchange() call might not fire your bound function:

这里有一个提示,为什么onchange()调用可能不会触发绑定函数:

http://jehiah.cz/archive/firing-javascript-events-properly

http://jehiah.cz/archive/firing-javascript-events-properly

#5


1  

What Chad says, except its better to use .keyup in this case because with .keydown and .keypress the value of the input is still the older value i.e. the newest key pressed would not be reflected if .val() is called.

Chad的意思是,除了最好使用。keyup,因为使用。keydown和。keypress输入的值仍然是旧值,也就是说,如果调用.val(),按下的最新键将不会被反映。

This should probably be a comment on Chad's answer but I dont have privileges to comment yet.

这可能是对查德的回答的评论,但我还没有权利评论。

#6


1  

I 2nd Chad Grant's answer and also submit this blog article [removed dead link] for your viewing pleasure.

我是查德·格兰特的第二份回复,同时也提交了这篇博客文章[删除死链接],供你浏览。

#7


0  

if you're trying to use jQuery autocomplete plugin, then I think you don't need to bind to onChange event, it will

如果您正在尝试使用jQuery自动完成插件,那么我认为您不需要绑定到onChange事件,它会的

#1


31  

You're looking for keydown/press/up

你正在寻找keydown /新闻/

$("#inputID").keydown(function(event) {
    alert(event.keyCode);
});

or using bind $("#inputID").bind('onkeydown', ...

或者使用绑定美元(“# inputID”)。绑定(“onkeydown”,…

http://docs.jquery.com/Events

http://docs.jquery.com/Events

#2


18  

You must change the event name from "change" to "onchange":

您必须将事件名称从“更改”更改为“onchange”:

$(document).ready(function(){
        $("input#tags").bind("onchange", autoFill);
          });

or use the shortcut binder method change:

或使用快捷绑定方法更改:

$(document).ready(function(){
        $("input#tags").change(autoFill);
          });

Note that the onchange event usually fires when the user leave the input, so for auto-complete you better use the keydown event.

注意,onchange事件通常在用户离开输入时触发,因此为了自动完成,最好使用keydown事件。

#3


7  

Combination of keyup and change is not necessarily enough (browser's autocomplete and paste using mouse also changes the contents of a text box, but doesn't fire either of these events):

按键和更改的组合不一定足够(浏览器的自动完成和使用鼠标粘贴也会更改文本框的内容,但不会触发这两个事件):

jquery change not working incase of dynamic value change

jquery在动态值更改的情况下不工作

#4


1  

Here's a clue why an onchange() call might not fire your bound function:

这里有一个提示,为什么onchange()调用可能不会触发绑定函数:

http://jehiah.cz/archive/firing-javascript-events-properly

http://jehiah.cz/archive/firing-javascript-events-properly

#5


1  

What Chad says, except its better to use .keyup in this case because with .keydown and .keypress the value of the input is still the older value i.e. the newest key pressed would not be reflected if .val() is called.

Chad的意思是,除了最好使用。keyup,因为使用。keydown和。keypress输入的值仍然是旧值,也就是说,如果调用.val(),按下的最新键将不会被反映。

This should probably be a comment on Chad's answer but I dont have privileges to comment yet.

这可能是对查德的回答的评论,但我还没有权利评论。

#6


1  

I 2nd Chad Grant's answer and also submit this blog article [removed dead link] for your viewing pleasure.

我是查德·格兰特的第二份回复,同时也提交了这篇博客文章[删除死链接],供你浏览。

#7


0  

if you're trying to use jQuery autocomplete plugin, then I think you don't need to bind to onChange event, it will

如果您正在尝试使用jQuery自动完成插件,那么我认为您不需要绑定到onChange事件,它会的