如何使用 JQuery 绑定 TextBox 的 onchange 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/805963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:49:19  来源:igfitidea点击:

How do i bind onchange event of a TextBox using JQuery?

jqueryhtml

提问by gath

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

如何使用 jQuery 将 TextBox 的 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 stackoverflow which detects OnChange events and calls AJAX functions when a user types in.

注意:我想实现一个 Tag TextBox,就像 stackoverflow 一样,它检测 OnChange 事件并在用户输入时调用 AJAX 函数。

回答by Chad Grant

You're looking for keydown/press/up

您正在寻找 keydown/press/up

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

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

或使用 bind $("#inputID").bind('onkeydown', ...

http://docs.jquery.com/Events

http://docs.jquery.com/Events

回答by Alex LE

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

您必须将事件名称从“change”更改为“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 事件。

回答by Jakub P.

Combination of keyupand changeis 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):

结合keyupchange不一定是足够的(浏览器的自动完成和使用鼠标也改变文本框的内容粘贴,但没有任何触发这些事件的):

jquery change not working incase of dynamic value change

jquery更改不起作用以防动态值更改

回答by Ibrahim Muhammad

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.

这可能应该是对乍得回答的评论,但我还没有评论的特权。

回答by spthorn

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

回答by vitaminjeff

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

我是 Chad Grant 的第二个回答,并提交了这篇博客文章 [已删除死链接] 以供您观看。

回答by iwan

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 事件,它会