Javascript IE7 / IE8差异

时间:2011-05-10 10:14:06

标签: javascript

当我在IE8中使用document.getElementById('checkbox1').checked == true它不起作用但在IE7中工作时,请问任何解决方案?

<script language="Javascript" type="text/javascript">
function swap(){ 
        if(document.getElementById('checkbox1').checked == true ){  
           document.getElementById('captionrow1').style.display = "none";
           document.getElementById('captionrow2').style.display = "inline";
            document.getElementById('show').style.display = "inline";

                        if (location.href.indexOf("CheckBox1=1") == -1)
                                location.href = "employees_commends1a.asp?CheckBox1=1";
         } 
   if(document.getElementById('checkbox1').checked == false ){  
            document.getElementById('captionrow1').style.display = "inline";
           document.getElementById('captionrow2').style.display = "none";
           document.getElementById('show').style.display = "none";
      } 
   }
</script>

1 个答案:

答案 0 :(得分:0)

确保该复选框具有唯一的ID此外,您的代码更改了位置=卸载页面 - 我确信这是使您的代码无效的原因。

我建议这样做:

window.onload=function() {
  var chk = document.getElementById('checkbox1');
  chk.checked=location.href.indexOf("CheckBox1=1") != -1
  chk.onclick=function() { 
    location = "employees_commends1a.asp?"+(this.checked?"CheckBox1=1":"CheckBox1=0");
  }
  swap();
}

function swap(){ 
  var checked = document.getElementById('checkbox1').checked;
  document.getElementById('captionrow1').style.display = (checked)?"none":"inline";
  document.getElementById('captionrow2').style.display = (checked)?"inline":"none";
  document.getElementById('show').style.display = (checked)?"inline":"none";
}

<input type="checkbox" id="checkbox1" />