我通过我的PHP代码生成了以下html
我想要做的是当我选择span“row”中的复选框时,“main_row”的复选框将包含在“row”中,
应该自动咀嚼。
此外,当我取消选中“main_row”复选框时,应取消选中“row:复选框”中包含的所有内容。
为此,我可以使用javascript或jquery。
<span id="mainTopHeading" >Imports & Exports</span>
<span id="lblmainTopHeading">Categories</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="Zubair & CO" recordID="1920" />
<span title="Zubair & CO" id="pHName">Zubair & CO</span>
<span title="Roland">Roland</span>
</span>
<span id="row" recordID="1920" curstomerName="Zubair & CO" >
<input type="checkbox" recordID="1920" curstomerName="Zubair & CO" />
<span>Order Rice & Sugar</span>
<span>Roland</span>
</span>
<span id="mainTopHeading" >Manufacturing</span>
<span id="lblmainTopHeading">Categories</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="Howard Manufacturing" recordID="1870" />
<span title="Howard Manufacturing" id="pHName">Howard Manufacturing</span>
<span title="Roland"> Roland</span>
</span>
<span id="row" index="1" recordID="1870" curstomerName="Howard Manufacturing" >
<input type="checkbox" style="left:10;" recordID="1870" curstomerName="Howard Manufacturing" />
<span>Order Tires and Plastic products</span>
<span>Roland</span>
</span>
<span id="row" index="1" recordID="1870" curstomerName="Howard Manufacturing" >
<input type="checkbox" style="left:10;" recordID="1870" curstomerName="Howard Manufacturing" />
<span>Order Electronics</span>
<span>Roland</span>
</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="James & Sons" recordID="1866" />
<span title="James & Sons" id="pHName">James & Sons</span>
<span title="Roland">Roland</span>
</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="Villa Thresa Inc" recordID="1866" />
<span title="Villa Thresa Inc" id="pHName">Villa Thresa Inc</span>
<span title="Roland">Roland</span>
</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="Bangkok Manufacturing" recordID="1866" />
<span title="Bangkok Manufacturing" id="pHName">Bangkok Manufacturing</span>
<span title="Roland">Roland</span>
</span>
<span id="row" recordID="1920" curstomerName="Zubair & CO" >
<input type="checkbox" recordID="1920" curstomerName="Zubair & CO" />
<span>Order Rice & Sugar</span>
<span>Roland</span>
</span>
<span id="mainTopHeading">Misc.</span>
<span id="lblmainTopHeading">Different things</span>
<span id="main_row" class="mystyleClass" >
<input type="checkbox" id="pH" curstomerName="Bangkok Manufacturing" recordID="1866" />
<span title="Bangkok Manufacturing" id="pHName">Bangkok Manufacturing</span>
<span title="Roland">Roland</span>
</span>
答案 0 :(得分:3)
上面提到的id
到class
更改@patrick后,您可以这样做:
$(".main_row :checkbox").change(function() {
$(this).closest(".main_row").nextUntil(".main_row")
.find(":checkbox").attr("checked", this.checked);
});
You can try a demo here,当您选中/取消选中.main_row
中的复选框时,它会升至其.main_row
,使用.nextUntil(".main_row")
获取所有<span>
单击的.main_row
和下一个之间的元素,所以此类别。然后它会在其中获取任何复选框,并将其checked
属性设置为与您单击的.main_row
中的复选框相同。
如果除.row
之外可能还有其他内容,您可以在.filter(".row")
之后添加.nextuntil()
,将其限制为.row
个元素。
答案 1 :(得分:3)
这是一个(我认为)做你想要的解决方案。
(以尼克的例子为出发点。我看到尼克发布了类似的解决方案,但这个方法采用了不同的方法。)
除了您要求的内容之外,如果您点击.main_row
下的复选框,它会选中/取消选中所有相关的.row
复选框。
试一试: http://jsfiddle.net/nykda/
$(".row input").change(function() {
var $row= $(this).closest(".row");
var $main_row = $row.prev('.main_row').length ? $row.prev('.main_row') : $row.prevUntil(".main_row").prev();
$main_row.find(":checkbox").attr("checked", function(i,attr) {
return $main_row.nextUntil('.main_row').filter(':has(input:checked)').length ? "checked" : false;
});
});
$(".main_row input").change(function() {
$(this).closest(".main_row").nextUntil('.main_row').children(':checkbox').attr('checked', this.checked);
});