开发者

Monitoring the check state of a checkbox

开发者 https://www.devze.com 2023-02-20 15:29 出处:网络
What is the best way to automatically check if a checkbox has been (un)checked. I know that the onclick event will catch click events but what if another script changes the state of the checkbox ala \

What is the best way to automatically check if a checkbox has been (un)checked. I know that the onclick event will catch click events but what if another script changes the state of the checkbox ala "chec开发者_高级运维k all button".


You can use the 'change' event. The behavior is buggy in IE, so you might have to observe both click and change - http://www.quirksmode.org/dom/events/change.html.


You can't use the change event to monitor for programmatic changes (changes, not originated from a click) to the checkbox. So you'll have to make up your own monitor. One way is to use setInterval for periodic checking. Something like: setInterval(checkstate,2000);. See this this jsfiddle to see an example in action.


using the checked property and the CheckboxStateChange event. I don't know about compatibility, though.

https://developer.mozilla.org/en/XUL/checkbox

https://developer.mozilla.org/en/XUL/Events

http://www.javascriptkit.com/javatutors/radiocheck.shtml


You can define one global variable in java script and change its state to checked
or unchecked on click that changes the state of check box and on another script changes
the state of the check box


The other scripts should initiate a checkbox click in code itself. The event will bubble even if initiated from script. Here is an example if you are interested --> http://jsfiddle.net/qSRzM/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号