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

后端 未结 9 874
猫巷女王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:35

    I believe you can manually trigger the change event with trigger():

    $("#single").val("Single2").trigger('change');
    

    Though why it doesn't fire automatically, I have no idea.

提交回复
热议问题