Why does the jquery change event not trigger when I set the value of a select using val()?

后端 未结 9 786
猫巷女王i
猫巷女王i 2020-11-22 09:34

The logic in the change() event handler is not being run when the value is set by val(), but it does run when user selects a value with their mouse

9条回答
  •  灰色年华
    2020-11-22 10:10

    In case you don't want to mix up with default change event you can provide your custom event

    $('input.test').on('value_changed', function(e){
        console.log('value changed to '+$(this).val());
    });
    

    to trigger the event on value set, you can do

    $('input.test').val('I am a new value').trigger('value_changed');
    

提交回复
热议问题