val()不会在jQuery中触发change()

val()不会在jQuery中触发change()

问题描述:

当我使用按钮更改其值时,我试图在文本框上触发change事件,但是它不起作用.检查此小提琴.

I'm trying to trigger the change event on a text box when I change its value with a button, but it doesn't work. Check this fiddle.

如果在文本框中键入内容,然后单击其他位置,则会触发change.但是,如果单击该按钮,则文本框的值会更改,但是change不会触发.为什么?

If you type something in the text boxes and click somewhere else, change is triggered. However, if you click the button, the text box value is changed, but change doesn't trigger. Why?

onchange仅在用户键入输入内容时触发,然后输入内容失去焦点.

onchange only fires when the user types into the input and then the input loses focus.

设置值后,您可以使用手动调用onchange事件:

You can manually call the onchange event using after setting the value:

$("#mytext").change(); // someObject.onchange(); in standard JS

或者,您可以使用以下方法触发该事件:

Alternatively, you can trigger the event using:

$("#mytext").trigger("change");