简体   繁体   English

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

[英]How to detect checkbox state in Javascript IE7

I have a simple checkbox on my website. 我的网站上有一个简单的复选框。 Everytime you check and uncheck it, it shows an alert as to what the state is. 每次您选中和取消选中它时,都会显示有关状态的警报。 This works flawlessly in FF, Chrome, and IE8. 在FF,Chrome和IE8中可以完美运行。 However, when I run this in IE7, no matter if the check is on or off, it always alerts "off". 但是,当我在IE7中运行此命令时,无论检查是打开还是关闭,它始终会发出“关闭”警报。 It's like IE7 can't detect that the checkbox is checked. 就像IE7无法检测到该复选框被选中一样。

Does anyone have an idea of how to fix this or work around it? 有谁知道如何解决或解决它的想法? I basically need to toggle the visibility of some content based on a checkbox, but IE7 is being difficult. 我基本上需要基于复选框切换某些内容的可见性,但是IE7很难。 Thanks! 谢谢!

Here is my code that I'm using: 这是我正在使用的代码:

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

EDIT: 编辑:
Okay, turns out that having the Name and the ID of the element confused the browser. 好的,事实证明,让元素的名称和ID混淆了浏览器。 I made the name of the element different than the ID and that solved it. 我使元素的名称与ID不同,从而解决了该问题。

Thank you both for helping me out. 谢谢你们俩帮助我。 Just hearing that you guys had tried it and couldn't replicate my problem told me that perhaps I was looking in the wrong place, of which I was :) 刚刚听到你们尝试过并且无法复制我的问题,这告诉我也许我在错误的地方寻找,我当时是:)

Okay, turns out that having both the Name and the ID of the element the same confused the browser. 好的,事实证明,元素的名称和ID相同会使浏览器感到困惑。 I made the name of the element different than the ID and that solved it. 我使元素的名称与ID不同,从而解决了该问题。

Hi there i wanted to help but I was unable to duplicate the problem.. below is the code I tried. 您好,我想提供帮助,但是我无法复制问题..以下是我尝试的代码。

<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>

This code works in IE7. 此代码在IE7中有效。 Perhaps you can post the code for the checkbox and for whatever event handler calls the "showHideTimeDropdown" function. 也许您可以为该复选框以及任何事件处理程序调用“ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM