Detecting value change of input[type=text] in jQuery

前端 未结 10 910
囚心锁ツ
囚心锁ツ 2020-12-04 07:04

I want to execute a function every time the value of a specific input box changes. It almost works with $(\'input\').keyup(function), but nothing happe

相关标签:
10条回答
  • 2020-12-04 07:57

    This combination of events worked for me:

    $("#myTextBox").on("input paste", function() {
       alert($(this).val()); 
    });
    
    0 讨论(0)
  • 2020-12-04 07:58

    DON'T FORGET THE cut or select EVENTS!

    The accepted answer is almost perfect, but it forgets about the cut and select events.

    cut is fired when the user cuts text (CTRL + X or via right click)

    select is fired when the user selects a browser-suggested option

    You should add them too, as such:

    $("#myTextBox").on("change paste keyup cut select", function() {
       //Do your function 
    });
    
    0 讨论(0)
  • 2020-12-04 08:01

    Try this.. credits to https://stackoverflow.com/users/1169519/teemu

    for answering my question here: https://stackoverflow.com/questions/24651811/jquery-keyup-doesnt-work-with-keycode-filtering?noredirect=1#comment38213480_24651811

    This solution helped me to progress on my project.

    $("#your_textbox").on("input propertychange",function(){
    
       // Do your thing here.
    });
    

    Note: propertychange for lower versions of IE.

    0 讨论(0)
  • 2020-12-04 08:03

    Description

    You can do this using jQuery's .bind() method. Check out the jsFiddle.

    Sample

    Html

    <input id="myTextBox" type="text"/>
    

    jQuery

    $("#myTextBox").bind("change paste keyup", function() {
       alert($(this).val()); 
    });
    

    More Information

    • jsFiddle Demonstration
    • jQuery.bind()
    0 讨论(0)
提交回复
热议问题