2011-05-10 39 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" />