我有一个包含多行的表,在此表中每行都有它的复选框和一个文本字段(此文本字段包含类XXX,但每行不同,尽管有时两个共享同一个类)。
现在我想要,当用户选择复选框1(文本字段具有类“A”)和复选框5(文本字段具有类“B”)并单击“选择组”按钮时,选择所有< / strong>其他复选框,其行中包含“A”类或“B”类的文本字段。
我在考虑使用$(this).closest
,但我不知道jQuery是如何工作的,所以有人能指出我正确的方向吗?
提前致谢。
编辑某些HTML:http://jsfiddle.net/hg4p6/1/
<table>
<tr>
<td>
<input type='checkbox' id='chk1'>
</td>
<td>
<input type='text' value='123' class='A'>
</td>
</tr>
<tr>
<td>
<input type='checkbox' id='chk2'>
</td>
<td>
<input type='text' value='518' class='C'>
</td>
</tr>
<tr>
<td>
<input type='checkbox' id='chk3'>
</td>
<td>
<input type='text' value='321' class='B'>
</td>
</tr>
<tr>
<td>
<input type='checkbox' id='chk4'>
</td>
<td>
<input type='text' value='567' class='A'>
</td>
</tr>
<tr>
<td>
<input type='checkbox' id='chk5'>
</td>
<td>
<input type='text' value='971' class='B'>
</td>
</tr>
<tr>
<td colspan='2'><input type='button' value='Select same classes' onclick='Funcion'>
</td>
</tr>
</table>
所以基本上,当你选择第一个复选框时,它应该选择第4个。如果您选择第3个复选框,则应选择第5个复选框。
答案 0 :(得分:1)
你去吧
正在使用 demo
$(function(){
$("#selectAll").click(function(){
var className, $table = $("table");
$table.find("input:checked")
.each(function(){
className = $(this).closest("tr").find("input:text").attr('class');
$table.find("input[type:text]."+className)
.closest("tr").find("input:checkbox").attr('checked', true);
});
});
});
答案 1 :(得分:1)
我的解决方案:
$(function() {
$('#clickme').click(function() {
$('input:checked').each(function() {
var c = ($(this).parent().next().children("input:text").attr("class"));
$("input:text").filter("."+c).parent().prev().children("input:checkbox").prop("checked",true);
});
});
});
答案 2 :(得分:0)
$(":button").click(function(){
$("input[type='checkbox']").each(function(){
var is = $(this).closest("td").next("td").children("input").is(".B,.A");
if(is)
$(this).prop("checked",true);
});
});