onchange event for input type=“number”

前端 未结 10 1783
梦如初夏
梦如初夏 2020-12-02 22:11

How can I handle an onchange for ? I can\'t do a keyup or keydown, be

10条回答
  •  情歌与酒
    2020-12-02 22:31

    There may be a better solution, but this is what came to mind:

    var value = $("#yourInput").val();
    $("#yourInput").on('keyup change click', function () {
        if(this.value !== value) {
            value = this.value;
            //Do stuff
        }        
    });
    

    Here's a working example.

    It simply binds an event handler to the keyup, change and click events. It checks whether or not the value has changed, and if so, stores the current value so it can check again next time. The check is required to deal with the click event.

提交回复
热议问题