onchange event for input type=“number”

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

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

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 22:13

    http://jsfiddle.net/XezmB/8/

    $(":input").bind('keyup change click', function (e) {
        if (! $(this).data("previousValue") || 
               $(this).data("previousValue") != $(this).val()
           )
       {
            console.log("changed");           
            $(this).data("previousValue", $(this).val());
       }
    
    });
    
    
    $(":input").each(function () {
        $(this).data("previousValue", $(this).val());
    });​
    

    This is a little bit ghetto, but this way you can use the "click" event to capture the event that runs when you use the mouse to increment/decrement via the little arrows on the input. You can see how I've built in a little manual "change check" routine that makes sure your logic won't fire unless the value actually changed (to prevent false positives from simple clicks on the field).

提交回复
热议问题