onchange event for input type=“number”

前端 未结 10 1771
梦如初夏
梦如初夏 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:11
    <input type="number" id="n" value="0" step=".5" />
    <input type="hidden" id="v" value = "0"/>
    
    <script>
    $("#n").bind('keyup mouseup', function () {
        var current = $("#n").val();
        var prevData = $("#v").val(); 
        if(current > prevData || current < prevData){
           $("#v").val(current);
           var newv = $("#v").val(); 
           alert(newv);
        } 
    });  
    </script>
    

    http://jsfiddle.net/patrickrobles53/s10wLjL3/

    I've used a hidden input type to be the container of the previous value that will be needed for the comparison on the next change.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-02 22:17

    The oninput event (.bind('input', fn)) covers any changes from keystrokes to arrow clicks and keyboard/mouse paste, but is not supported in IE <9.

    jQuery(function($) {
      $('#mirror').text($('#alice').val());
    
      $('#alice').on('input', function() {
        $('#mirror').text($('#alice').val());
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <input id="alice" type="number" step="any" value="99">
    
    <p id="mirror"></p>

    0 讨论(0)
  • 2020-12-02 22:18
    $("input[type='number']").bind("focus", function() {
        var value = $(this).val();
        $(this).bind("blur", function() {
            if(value != $(this).val()) {
                alert("Value changed");
            }
            $(this).unbind("blur");
        });
    });
    

    OR

    $("input[type='number']").bind("input", function() {
        alert("Value changed");
    });
    
    0 讨论(0)
  • 2020-12-02 22:24
    $(':input').bind('click keyup', function(){
        // do stuff
    });
    

    Demo: http://jsfiddle.net/X8cV3/

    0 讨论(0)
  • 2020-12-02 22:30

    Because $("input[type='number']") doesn't work on IE, we should use a class name or id, for example, $('.input_quantity').

    And don't use .bind() method. The .on() method is the preferred method for attaching event handlers to a document.

    So, my version is:

    HTML

    <input type="number" value="5" step=".5" min="1" max="999" id="txt_quantity" name="txt_quantity" class="input_quantity">
    

    jQuery

    <script>
    $(document).ready(function(){
        $('.input_quantity').on('change keyup', function() {
            console.log('nice');
        }); 
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题