2012-12-28 103 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; 
}