Javascript change event on input element fires on only losing focus

前端 未结 7 2271
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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.

提交回复
热议问题