Javascript 复选框中的 onchange / onclick 在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3117716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:19:25  来源:igfitidea点击:

onchange / onclick in a checkbox doesn't work in IE

javascripthtmlinternet-explorer

提问by Francisco Freitas

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. I've tried to change the event to chkbx_send_demo.onclick and it still won't work.

但它在 IE 中不起作用。我尝试将事件更改为 chkbx_send_demo.onclick,但它仍然无法正常工作。

回答by GOsha

Internet Explorer only fires the onchange event when the checkbox loses the focus (onblur). also see here:
http://krijnhoetmer.nl/stuff/javascript/checkbox-onchange/
and here:
http://bytes.com/topic/javascript/answers/92116-onchange-checkbox

Internet Explorer 仅在复选框失去焦点 (onblur) 时触发 onchange 事件。另请参阅此处:
http: //krijnhoetmer.nl/stuff/javascript/checkbox-onchange/
和此处:http:
//bytes.com/topic/javascript/answers/92116-onchange-checkbox

回答by hetaoblog

i faced the same issue on ie8, i used below trick to fix it. http://sleeplesscoding.blogspot.com/2010/01/fixing-ie-onchange-event-for-checkboxes.html

我在 ie8 上遇到了同样的问题,我用下面的技巧来解决它。 http://sleeplesscoding.blogspot.com/2010/01/fixing-ie-onchange-event-for-checkboxes.html

回答by Nivas

Are you sure onclick does not work? Did you check for javascript errors?

你确定 onclick不起作用?您是否检查了 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. OnChange works differently in IE, see GOsha's answer.

注意:这仅适用于点击。OnChange 在 IE 中的工作方式不同,请参阅 GOsha 的回答。

回答by Francisco Freitas

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 上执行,我真的不明白为什么。