Can jQuery check whether input content has changed?

前端 未结 9 1035
轮回少年
轮回少年 2020-12-23 19:30

Is it possible to bind javascript (jQuery is best) event to \"change\" form input value somehow?

I know about .change() method, but it does not trigger

9条回答
  •  轮回少年
    2020-12-23 19:52

    Since the user can go into the OS menu and select paste using their mouse, there is no safe event that will trigger this for you. The only way I found that always works is to have a setInterval that checks if the input value has changed:

    var inp = $('#input'),
        val = saved = inp.val(),
        tid = setInterval(function() {
            val = inp.val();
            if ( saved != val ) {
                console.log('#input has changed');
                saved = val;
        },50);
    

    You can also set this up using a jQuery special event.

提交回复
热议问题