如何可靠地检测何时检查复选框,无论如何? [英] How to reliabily detect when a checkbox has been checked, no matter how?

查看:88
本文介绍了如何可靠地检测何时检查复选框,无论如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来检测复选框已选中的值是否已更改。 ()或者jQuery on()主要工作,但也不会检测到这样做的更改:

I'm looking for a way to detect when a checkbox checked value has been changed. addEventListener() or jQuery on() mostly work, but neither will detect a change made this way:

<input type="checkbox" id="testCheckbox">

<!--- some other script further down the page, not under my control -->
<script type="text/javascript">
   document.getElementById("testCheckbox").checked = true;
</script>

有没有办法可以检测到这种变化?即使是在最新浏览器中工作的东西也是很棒的。

Is there a way I can detect this change ? Even something that only works in the latest browsers would be great.

编辑:要清楚,我想检测页面上任何脚本所做的更改。我不一定控制他们,所以我不能自己触发一个事件。

To be clear, I want to detect changes made by any script on the page. I don't necessarily control them, so I can't trigger an event myself.

推荐答案

,我会说),你可以在IE中使用它专有的 propertychange 事件。在其他浏览器中,我确信你现在唯一的选择是轮询。

For what it's worth (not much, I'd say), you can do this in IE only using its proprietary propertychange event. In other browsers, I'm pretty certain your only option for now is polling.

演示: http://jsfiddle.net/X4a2N/1/

代码:

document.getElementById("testCheckbox").onpropertychange = function(evt) {
    evt = evt || window.event;
    if (evt.propertyName == "checked") {
        alert("Checkedness changed!");
    }
};

这篇关于如何可靠地检测何时检查复选框,无论如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆