val() 不会触发 jQuery 中的 change() [英] val() doesn't trigger change() in jQuery
问题描述
当我使用按钮更改文本框的值时,我试图在文本框上触发 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").val( 777 ).change(); // someObject.onchange(); in standard JS
或者,您可以使用以下方法触发事件:
Alternatively, you can trigger the event using:
$("#mytext").val( 777 ).trigger("change");
这篇关于val() 不会触发 jQuery 中的 change()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!