2011-05-10 38 views
0

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

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

边注:在'language'属性已被弃用(https://developer.mozilla.org/en/HTML/Element/script) – 2011-05-10 10:32:51

回答

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