2012-12-28 149 views
0

表我有一個表像這樣在我的asp.net MVC視圖編碼:循環訪問複選框使用jQuery

<table> 
    <tr> 
    <td>Option 1<td> 
    <td>Option 2<td> 
    <td>Option 3<td> 
    <td>Option 4<td> 
    <tr> 
foreach(var options in Model) 
{ 
    <tr> 
    <td>@Html.ChecBoxFor(x => x.one)<td> 
    <td>@Html.ChecBoxFor(x => x.two)<td> 
    <td>@Html.ChecBoxFor(x => x.three)<td> 
    <td>@Html.ChecBoxFor(x => x.four)<td> 
    <tr> 
} 
<table> 

我試圖找出如何通過每一行迭代,然後有條件切換某些複選框。例如,如果複選框1被選中,則確保複選框4未被選中,反之亦然。條件將分別適用於複選框2和3。

+3

類似的事情已經做什麼單選按鈕? –

回答

0

在JSFiddle中試過了這個。老生常談看到demo

$(function() { 
    $("#btn").click(function() { 
     $("tr").each(function() { 
      var $RowCheckBoxes = $(this).find("input:checkbox"); 

      // if checkbox one is checked then make sure checkbox four is unchecked 
      if ($RowCheckBoxes.eq(0).is(':checked') == true) { 
       $RowCheckBoxes.eq(3).prop("checked", false); 
      } 

      // The conditon would apply to checkboxes two and three respectively. 
      if ($RowCheckBoxes.eq(1).is(':checked') == true) { 
       $RowCheckBoxes.eq(2).prop("checked", false); 
      } 
     }); 
    }); 
}); 
+0

這對我有用。我做了一個改變,以便它可以在飛行中檢查。我改變了$(「btn」),點擊$(「input:checkbox」)。 – user1206480

0

你不需要jQuery的這個基本任務,只是給你檢查框的名稱和:

var chk = document.getElementsByName("theName"); 
for(var i = 0; i < chk.length; i++){ 
    chk[i].whateverYouWant; 
}