Google Chrome change event in input is not fired if char added on keyup

前端 未结 5 1175
心在旅途
心在旅途 2021-01-12 14:31
$(\'input#not-gonna-work\').bind({
    keyup: function(){
        console.log(\'Typed a key\');
        $(this).val($(this).val() + \'.\');// try with any other char         


        
5条回答
  •  执笔经年
    2021-01-12 15:22

    This looks like a valid Chrome bug. My guess is that changing the value resets whatever Chrome relies on to trigger the onChange event since the user last focused on the input. I would suggest using a workaround (unfortunately) that should work in all browsers.

    I've updated your code with such an example here. The idea is that you store the original value when the input receives focus, then compare the original value to the updated value when the input is blurred. If it is a different value, then perform the same logic that you would have put into your change callback.

提交回复
热议问题