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

前端 未结 5 1245
心在旅途
心在旅途 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:08

    That seems to be the behavior to me.. If the value of an element is changed via script then your onchange will not be triggered. In such cases you need to save the value onfocus and check the value onblur and trigger onchange manually.

    See below,

    DEMO: http://jsfiddle.net/vCxme/6/

    $('input#not-gonna-work').bind({
        keyup: function(){
            console.log('Typed a key');
            $(this).val($(this).val() + '.');
        },
        change: function(){
           console.log('I\'m a changed input');
        },
        focus: function ( ){
            $(this).data('orig_value', $(this).val());
        },
        blur: function () {
            if ($(this).val() != $(this).data('orig_value')){
                $(this).change();
            }
        }
    });
    $('input#gonna-work').bind({
        keyup: function(){
            console.log('Typed a key');
        },
        change: function(){
           console.log('I\'m a changed input');
        }
    });
    

    The above implementation is from pseudo code as from FF onChange

    Note: The browser change event will not be triggered when the value is changed via script.

提交回复
热议问题