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

时间:2020-12-02 21:44:57

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,它仍然不能工作。

4 个解决方案

#1


16  

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。问/材料/ javascript / checkbox-onchange /这里:http://bytes.com/topic/javascript/answers/92116-onchange-checkbox

#2


4  

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

#3


1  

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.

注意:这只适用于onclick。OnChange在IE中工作不同,见GOsha的回答。

#4


0  

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

#1


16  

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。问/材料/ javascript / checkbox-onchange /这里:http://bytes.com/topic/javascript/answers/92116-onchange-checkbox

#2


4  

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

#3


1  

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.

注意:这只适用于onclick。OnChange在IE中工作不同,见GOsha的回答。

#4


0  

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