How to detect a textbox's content has changed

后端 未结 15 1920
猫巷女王i
猫巷女王i 2020-11-22 16:10

I want to detect whenever a textbox\'s content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow ke

15条回答
  •  死守一世寂寞
    2020-11-22 16:24

    Use the onchange event in HTML/standard JavaScript.

    In jQuery that is the change() event. For example:

    $('element').change(function() { // do something } );

    EDIT

    After reading some comments, what about:

    $(function() {
        var content = $('#myContent').val();
    
        $('#myContent').keyup(function() { 
            if ($('#myContent').val() != content) {
                content = $('#myContent').val();
                alert('Content has been changed');
            }
        });
    });
    

提交回复
热议问题