Using JQuery, how do you detect if the value of a text input has changed while the field still has focus?

后端 未结 2 1759
小蘑菇
小蘑菇 2021-01-17 22:23

I noticed prior to posting this question that there have been similar questions posted on this topic before, however the user isn\'t interacting with the text field by using

相关标签:
2条回答
  • 2021-01-17 22:40

    You can just listen for the keypress event.

    <input type="text" id="target" value="" />
    
    var target = $('#target'),
        val = target.val();
    
    function monitor()
    {
        var current_val = $(this).val();
        if (current_val != val) {
            console.log('changed from', val, 'to', current_val);
            val = current_val;
        }
    }
    
    target.keypress(monitor);
    
    0 讨论(0)
  • 2021-01-17 22:41

    Well, there's always brute force: poll it with a setInterval().

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