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

后端 未结 7 977
我在风中等你
我在风中等你 2020-12-09 15:44

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

Am trying this and its failing:

$(document).ready(function() {
    $(\"input#         


        
相关标签:
7条回答
  • 2020-12-09 16:01

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

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

    0 讨论(0)
  • 2020-12-09 16:01

    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.

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

    0 讨论(0)
  • 2020-12-09 16:01

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

    0 讨论(0)
  • 2020-12-09 16:07

    You must change the event name from "change" to "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.

    0 讨论(0)
  • 2020-12-09 16:11

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

    0 讨论(0)
  • 2020-12-09 16:13

    You're looking for keydown/press/up

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

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

    http://docs.jquery.com/Events

    0 讨论(0)
提交回复
热议问题