代码之家  ›  专栏  ›  技术社区  ›  justinl

如何在Javascript IE7中检测复选框状态

  •  2
  • justinl  · 技术社区  · 16 年前

    有人知道如何解决这个问题吗?我基本上需要切换一些内容的可见性基于复选框,但IE7是困难的。谢谢!

    下面是我正在使用的代码:

    function showHideTimeDropdown()  
    {   
      if (document.getElementById("input_checkbox").checked == true){
        alert("on");
      }
      else{  
        alert("off");  
      }  
    }
    

    编辑:

    谢谢你们俩帮我。刚听说你们试过了,不能复制我的问题,我就告诉我,也许我找错地方了,我是:)

    3 回复  |  直到 16 年前
        1
  •  4
  •   justinl    16 年前

    好吧,结果是让元素的名称和ID都相同会让浏览器感到困惑。我使元素的名称与ID不同,从而解决了它。

        2
  •  1
  •   John Hartsock    16 年前

    <html> 
    <head> 
      <script language="javascript"> 
      function showHideTimeDropdown()   
    {    
      if (document.getElementById("input_checkbox").checked == true){ 
        alert("on"); 
      } 
      else{   
        alert("off");   
      }   
    } 
    window.onload = function () {showHideTimeDropdown();};
      </script> 
    </head> 
    <body> 
    
    
    <input id="input_checkbox" type="checkbox" value="true" onchange="showHideTimeDropdown();">
    <input type=text>
    </body> 
    </html>
    
        3
  •  1
  •   Steven P.    16 年前

    这个代码在IE7中工作。也许您可以发布复选框和任何事件处理程序调用“showHideTimeDropdown”函数的代码。

    <html>
      <head>
        <script>
          function showHideTimeDropdown()  
          {
            if (document.getElementById("input_checkbox").checked)
              alert("on");
            else
              alert("off");
          }
        </script>
      </head>
      <body>
        <input onclick="showHideTimeDropdown();" id="input_checkbox" type="checkbox"/>
      </body>
    </html>
    
    推荐文章