Javascript change event on input element fires on only losing focus

前端 未结 7 2332
爱一瞬间的悲伤
爱一瞬间的悲伤 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());
            }
        });
    

提交回复
热议问题