How do I implement onchange of <input type=“text”> with jQuery?

后端 未结 14 1041
[愿得一人]
[愿得一人] 2020-11-27 10:15

?

相关标签:
14条回答
  • 2020-11-27 10:53

    You can use .change()

    $('input[name=myInput]').change(function() { ... });
    

    However, this event will only fire when the selector has lost focus, so you will need to click somewhere else to have this work.

    If that's not quite right for you, you could use some of the other jQuery events like keyup, keydown or keypress - depending on the exact effect you want.

    0 讨论(0)
  • 2020-11-27 10:54

    If you want to trigger the event as you type, use the following:

    $('input[name=myInput]').on('keyup', function() { ... });
    

    If you want to trigger the event on leaving the input field, use the following:

    $('input[name=myInput]').on('change', function() { ... });
    
    0 讨论(0)
  • 2020-11-27 10:57

    You could simply work with the id

    $("#your_id").on("change",function() {
        alert(this.value);
    });
    
    0 讨论(0)
  • 2020-11-27 11:02

    There is one and only one reliable way to do this, and it is by pulling the value in an interval and comparing it to a cached value.

    The reason why this is the only way is because there are multiple ways to change an input field using various inputs (keyboard, mouse, paste, browser history, voiceinput etc.) and you can never detect all of them using standard events in a cross-browser environment.

    Luckily, thanks to the event infrastructure in jQuery, it’s quite easy to add your own inputchange event. I did so here:

    $.event.special.inputchange = {
        setup: function() {
            var self = this, val;
            $.data(this, 'timer', window.setInterval(function() {
                val = self.value;
                if ( $.data( self, 'cache') != val ) {
                    $.data( self, 'cache', val );
                    $( self ).trigger( 'inputchange' );
                }
            }, 20));
        },
        teardown: function() {
            window.clearInterval( $.data(this, 'timer') );
        },
        add: function() {
            $.data(this, 'cache', this.value);
        }
    };
    

    Use it like: $('input').on('inputchange', function() { console.log(this.value) });

    There is a demo here: http://jsfiddle.net/LGAWY/

    If you’re scared of multiple intervals, you can bind/unbind this event on focus/blur.

    0 讨论(0)
  • 2020-11-27 11:05
    $("input").keyup(function () {
        alert("Changed!");
    });
    
    0 讨论(0)
  • 2020-11-27 11:06
    <input id="item123" class="firstName" type="text" value="Hello there" data-someattr="CoolExample" />
    
    $(".firstName").on('change keyup paste', function () {
       var element = $(this);
       console.log(element);
       var dataAttribute = $(element).attr("data-someattr");
       console.log("someattr: " + dataAttribute );
    });
    

    I recommend use this keyword in order to get access to the entire element so your are able do everything you need with this element.

    0 讨论(0)
提交回复
热议问题