How to detect a textbox's content has changed

后端 未结 15 1899
猫巷女王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:21

    This Code detects whenever a textbox's content has changed by the user and modified by Javascript code.

    var $myText = jQuery("#textbox");
    
    $myText.data("value", $myText.val());
    
    setInterval(function() {
        var data = $myText.data("value"),
        val = $myText.val();
    
        if (data !== val) {
            console.log("changed");
            $myText.data("value", val);
        }
    }, 100);
    
    0 讨论(0)
  • 2020-11-22 16:24

    Use the onchange event in HTML/standard JavaScript.

    In jQuery that is the change() event. For example:

    $('element').change(function() { // do something } );

    EDIT

    After reading some comments, what about:

    $(function() {
        var content = $('#myContent').val();
    
        $('#myContent').keyup(function() { 
            if ($('#myContent').val() != content) {
                content = $('#myContent').val();
                alert('Content has been changed');
            }
        });
    });
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-22 16:27

    I'd like to ask why you are trying to detect when the content of the textbox changed in real time?

    An alternative would be to set a timer (via setIntval?) and compare last saved value to the current one and then reset a timer. This would guarantee catching ANY change, whether caused by keys, mouse, some other input device you didn't consider, or even JavaScript changing the value (another possiblity nobody mentioned) from a different part of the app.

    0 讨论(0)
  • 2020-11-22 16:28

    The 'change' event doesn't work correctly, but the 'input' is perfect.

    $('#your_textbox').bind('input', function() {
        /* This will be fired every time, when textbox's value changes. */
    } );
    
    0 讨论(0)
  • 2020-11-22 16:28
    document.getElementById('txtrate' + rowCount).onchange = function () {            
           // your logic
    };
    

    This one works fine but triggers the event on click too which is not good. my system went into loop. while

    $('#txtrate'+rowCount).bind('input', function() {
            //your logic
    } );
    

    works perfectly in my scenario. it only works when value is changed. instead of $ sign one can use document.getElementById too

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