How to detect a textbox's content has changed

后端 未结 15 1896
猫巷女王i
猫巷女王i 2020-11-22 16:10

I want to detect whenever a textbox\'s content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow ke

15条回答
  •  有刺的猬
    2020-11-22 16:25

    I would recommend taking a look at jQuery UI autocomplete widget. They handled most of the cases there since their code base is more mature than most ones out there.

    Below is a link to a demo page so you can verify it works. http://jqueryui.com/demos/autocomplete/#default

    You will get the most benefit from reading the source and seeing how they solved it. You can find it here: https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.autocomplete.js.

    Basically they do it all, they bind to input, keydown, keyup, keypress, focus and blur. Then they have special handling for all sorts of keys like page up, page down, up arrow key and down arrow key. A timer is used before getting the contents of the textbox. When a user types a key that does not correspond to a command (up key, down key and so on) there is a timer that explorers the content after about 300 milliseconds. It looks like this in the code:

    // switch statement in the 
    switch( event.keyCode ) {
                //...
                case keyCode.ENTER:
                case keyCode.NUMPAD_ENTER:
                    // when menu is open and has focus
                    if ( this.menu.active ) {
                        // #6055 - Opera still allows the keypress to occur
                        // which causes forms to submit
                        suppressKeyPress = true;
                        event.preventDefault();
                        this.menu.select( event );
                    }
                    break;
                default:
                    suppressKeyPressRepeat = true;
                    // search timeout should be triggered before the input value is changed
                    this._searchTimeout( event );
                    break;
                }
    // ...
    // ...
    _searchTimeout: function( event ) {
        clearTimeout( this.searching );
        this.searching = this._delay(function() { // * essentially a warpper for a setTimeout call *
            // only search if the value has changed
            if ( this.term !== this._value() ) { // * _value is a wrapper to get the value *
                this.selectedItem = null;
                this.search( null, event );
            }
        }, this.options.delay );
    },
    

    The reason to use a timer is so that the UI gets a chance to be updated. When Javascript is running the UI cannot be updated, therefore the call to the delay function. This works well for other situations such as keeping focus on the textbox (used by that code).

    So you can either use the widget or copy the code into your own widget if you are not using jQuery UI (or in my case developing a custom widget).

提交回复
热议问题