Can jQuery check whether input content has changed?

前端 未结 9 1065
轮回少年
轮回少年 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:50

    Yes, compare it to the value it was before it changed.

    var previousValue = $("#elm").val();
    $("#elm").keyup(function(e) {
        var currentValue = $(this).val();
        if(currentValue != previousValue) {
             previousValue = currentValue;
             alert("Value changed!");
        }
    });
    

    Another option is to only trigger your changed function on certain keys. Use e.KeyCode to figure out what key was pressed.

提交回复
热议问题