Javascript change event on input element fires on only losing focus

前端 未结 7 2140
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 15:45

I have an input element and I want to keep checking the length of the contents and whenever the length becomes equal to a particular size, I want to enable the submit button

相关标签:
7条回答
  • 2020-11-27 16:03

    As an extention to katspaugh's answer, here's a way to do it for multiple elements using a css class.

       $('.myclass').each(function(){
            $(this).attr('oldval',$(this).val());
       });
    
       $('.myclass').on('change keypress paste focus textInput input',function(){
           var val = $(this).val();
           if(val != $(this).attr('oldval') ){
               $(this).attr('oldval',val); 
               checkLength($(this).val());
            }
        });
    
    0 讨论(0)
  • 2020-11-27 16:05
    (function () {
        var oldVal;
    
        $('#name').on('change textInput input', function () {
            var val = this.value;
            if (val !== oldVal) {
                oldVal = val;
                checkLength(val);
            }
        });
    }());
    

    This will catch change, keystrokes, paste, textInput, input (when available). And not fire more than necessary.

    http://jsfiddle.net/katspaugh/xqeDj/


    References:

    textInput — a W3C DOM Level 3 event type. http://www.w3.org/TR/DOM-Level-3-Events/#events-textevents

    A user agent must dispatch this event when one or more characters have been entered. These characters may originate from a variety of sources, e.g., characters resulting from a key being pressed or released on a keyboard device, from the processing of an input method editor, or resulting from a voice command. Where a “paste” operation generates a simple sequence of characters, i.e., a text passage without any structure or style information, this event type should be generated as well.

    input — an HTML5 event type.

    Fired at controls when the user changes the value

    Firefox, Chrome, IE9 and other modern browsers support it.

    This event occurs immediately after modification, unlike the onchange event, which occurs when the element loses focus.

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

    You would have to use a combination of onkeyup and onclick (or onmouseup) if you want to catch every possibility.

    <input id="name" onkeyup="checkLength(this.value)" onmouseup="checkLength(this.value)" />
    
    0 讨论(0)
  • 2020-11-27 16:10

    You can use onkeyup

    <input id="name" onkeyup="checkLength(this.value)" />
    
    0 讨论(0)
  • 2020-11-27 16:18

    Do it the jQuery way:

    <input type="text" id="name" name="name"/>
    
    
    $('#name').keyup(function() {
      alert('Content length has changed to: '+$(this).val().length);
    });
    
    0 讨论(0)
  • 2020-11-27 16:19

    Here is another solution I develop for the same problem. However I use many input boxes so I keep old value as an user-defined attribute of the elements itself: "data-value". Using jQuery it is so easy to manage.

            $(document).delegate('.filterBox', 'keyup', { self: this }, function (e) {
                var self = e.data.self;
    
                if (e.keyCode == 13) {
                    e.preventDefault();
                    $(this).attr('data-value', $(this).val());
                    self.filterBy(this, true)
                }
                else if (e.keyCode == 27) {
                    $(this).val('');
                    $(this).attr('data-value', '');
                    self.filterBy(this, true)
                }
                else {
                    if ($(this).attr('data-value') != $(this).val()) {
                        $(this).attr('data-value', $(this).val());
                        self.filterBy(this);
                    }
                }
            });
    

    here is, I used 5-6 input boxes have class 'filterBox', I make filterBy method run only if data-value is different than its own value.

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