2009-01-23 99 views
27

我有一個javascript例程在一組複選框上執行操作,但最終操作我想將單擊複選框設置爲選中或取消選中,如果用戶是選中該框或取消選中。使用Javascript來檢查複選框是否被選中或取消選中

不幸的是,每次我檢查它是否被檢查或未檢查時,它都會返回「on」,表示用戶總是檢查該框!任何幫助將不勝感激,我也包括了JavaScript。

// Uncheck all the checkboxs with the same Tax Credit 
for (i=0; i<arrChecks.length; i++) 
{ 
    var attribute = arrChecks[i].getAttribute("xid") 
    if (attribute == elementName) 
    { 
     // if the current state is checked, unchecked and vice-versa 
     if (arrChecks[i].value == "on") // <-- This is always returning true, even if the box is being unchecked 
     { 
      arrChecks[i].checked = 1; 
     } else { 
      arrChecks[i].checked = 0; 
     } 

    } else { 
     arrChecks[i].checked = 0; 
    } 
} 
+0

在html中檢查複選框的正確方法是checked =「checked」 – 2009-01-23 16:46:01

+1

您是否考慮過像jquery,prototype,moo或yui這樣的庫?他們使這個更容易 – 2009-01-23 19:18:45

回答

24

您應該根據複選框元素的checked屬性進行評估。

for (i=0; i<arrChecks.length; i++) 
{ 
    var attribute = arrChecks[i].getAttribute("xid") 
    if (attribute == elementName) 
    { 
     // if the current state is checked, unchecked and vice-versa 
     if (arrChecks[i].checked) 
     { 
      arrChecks[i].checked = false; 
     } else { 
      arrChecks[i].checked = true; 
     } 

    } else { 
     arrChecks[i].checked = false; 
    } 
} 
+1

謝謝你們,這個作品非常完美! – 2009-01-23 18:29:34

17

一個checkboxvalue屬性是什麼,你的設置:

<input type='checkbox' name='test' value='1'> 

因此,當有人將檢查中,服務器接收一個名爲test變量,1value - 你想要什麼檢查它是不是value它(它將永遠不會更改,無論是否檢查),但複選框的狀態爲checked

所以,如果你將這段代碼:

if (arrChecks[i].value == "on") 
{ 
    arrChecks[i].checked = 1; 
} else { 
    arrChecks[i].checked = 0; 
} 

有了這個:

arrChecks[i].checked = !arrChecks[i].checked; 

它應該工作。您應該使用truefalse而不是01

+0

因此,代碼將會出現:「if(arrChecks [i] .checked)arrChecks [i] .checked = 1;」。那也不對。 – 2009-01-23 17:46:09

+1

夠公平的。沒有看到那裏的邏輯。固定。 – 2009-01-23 19:07:50

0

我不確定是什麼問題,但我很確定這會解決它。

for (i=0; i<arrChecks.length; i++) 
    { 
     var attribute = arrChecks[i].getAttribute("xid") 
     if (attribute == elementName) 
     { 
      if (arrChecks[i].checked == 0) 
      { 
       arrChecks[i].checked = 1; 
      } else { 
       arrChecks[i].checked = 0; 
      } 

     } else { 
      arrChecks[i].checked = 0; 
     } 
    } 
0

此外,請確保你在Firefox和IE中測試它。 JS操作複選框有一些令人討厭的錯誤。

5

要切換複選框,或者您可以使用

element.checked = !element.checked; 

,所以你可以使用

if (attribute == elementName) 
{ 
    arrChecks[i].checked = !arrChecks[i].checked; 
} else { 
    arrChecks[i].checked = false; 
} 
4
function enter_comment(super_id) { 
    if (!(document.getElementById(super_id).checked)) { 
     alert('selected checkbox is unchecked now') 
    } else { 
     alert('selected checkbox is checked now'); 
    } 
} 

<input type="checkbox" name="a" id="1" value="1" onclick="enter_comment(this.value)" />

<input type="checkbox" name="b" id="2" value="2" onclick="enter_comment(this.value)" />

3
function CHeck(){ 
    var ChkBox = document.getElementById("CheckBox1"); 
    alert(ChkBox.Checked); 
} 

<asp:CheckBox ID="CheckBox1" runat="server" onclick="CHeck()" /> 
相關問題