What event can be captured when an HTML hidden input value is set / changed

巧了我就是萌 提交于 2019-12-22 02:48:15

问题


HI, In JavaScript when value is set to a hidden input control, which event is fired?


回答1:


Whenever you change the value of a hidden field using script, it wont fire any event. But you can manually trigger the event if you are using jQuery.

Lets assume that you have the following hidden field

<input type="hidden" id="hid" value="0" 
onchange="alert('Caught the hidden event');" />

When you change the value of the field using following code, it will not display the alert message.

$("#hid").val("2");

But you can trigger the change event using the following code

$("#hid").val("2").change();

Above code will display the alert message.




回答2:


A value (aside from the initial value) can only be set on a hidden input by using scripting, and events do not generally fire in response to scripts.

It might trigger a Mutation event, but browser support for them is not all that widespread yet.

In general, if you want to do something when you script changes the value of a hidden input — make the script do the other thing at the same time.




回答3:


I'm guessing that 'onchange' would fire.



来源:https://stackoverflow.com/questions/2026704/what-event-can-be-captured-when-an-html-hidden-input-value-is-set-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!