[英]onchange / onclick in a checkbox doesn't work in IE
I have the following code, which works perfectly in Chrome/FF: 我有以下代码,它在Chrome / FF中完美运行:
chkbx_send_demo.onchange = function(){
if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
alert("Choose a Template");
sel_template.selectedIndex = 1;
}
if(chkbx_send_demo.checked == false){
sel_template.selectedIndex = 0;
}
}
But it just won't work in IE. 但它只是在IE中不起作用。 I've tried to change the event to chkbx_send_demo.onclick and it still won't work.
我试图将事件更改为chkbx_send_demo.onclick,它仍然无法正常工作。
Internet Explorer only fires the onchange event when the checkbox loses the focus (onblur). 当复选框失去焦点(onblur)时,Internet Explorer仅触发onchange事件。 also see here:
另见:
http://krijnhoetmer.nl/stuff/javascript/checkbox-onchange/ http://krijnhoetmer.nl/stuff/javascript/checkbox-onchange/
and here: 和这里:
http://bytes.com/topic/javascript/answers/92116-onchange-checkbox http://bytes.com/topic/javascript/answers/92116-onchange-checkbox
i faced the same issue on ie8, i used below trick to fix it. 我在ie8上遇到了同样的问题,我用下面的技巧来修复它。 http://sleeplesscoding.blogspot.com/2010/01/fixing-ie-onchange-event-for-checkboxes.html
http://sleeplesscoding.blogspot.com/2010/01/fixing-ie-onchange-event-for-checkboxes.html
Are you sure onclick does not work ? 你确定onclick 不起作用吗? Did you check for javascript errors?
你检查过javascript错误了吗?
The following works in IE7 (don't have IE6 to test) 以下在IE7中工作(没有IE6进行测试)
<html>
<head>
<script>
function text()
{
alert(document.getElementById("cbxTest").checked);
}
</script>
</head>
<body>
<input type="checkbox" name="cbxTest" id="cbxTest" onclick="text()"/>
<label for="cbxTest"> Test </label>
</body>
</html>
Note: This is only for onclick. 注意:这仅适用于onclick。 OnChange works differently in IE, see GOsha's answer.
OnChange在IE中的工作方式不同,请参阅GOsha的回答。
my JS code is now something like this: 我的JS代码现在是这样的:
if(navigator.appName == "Microsoft Internet Explorer"){
alert("IE");
chkbx_send_demo.onclick = function(){
alert("HI");
if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
alert("Choose a Template");
sel_template.selectedIndex = 1;
}
if(chkbx_send_demo.checked == false){
alert("HI");
sel_template.selectedIndex = 0;
}
alert("HI");
}
}
else
{
chkbx_send_demo.onchange = function(){
if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
alert("Choose a Template");
sel_template.selectedIndex = 1;
}
if(chkbx_send_demo.checked == false){
sel_template.selectedIndex = 0;
}
}
}
No javascript errors, but the code just isn't executed on IE and i really can't understand why. 没有javascript错误,但代码只是没有在IE上执行,我真的不明白为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.