问题描述
我试图遍历每个文本输入字段并找出分配给它的复选框是否被选中。 我在选择each()周期内的复选框时遇到了麻烦。
我的HTML:
<table>
<tbody>
<tr>
<td style="vertical-align: top;">
<label for="MainTemplate" class="">Hlavná ?ablóna:</label>
</td>
<td>
<input name="MainTemplate" class="input mainTemplate" value="" style="width: 10em;" type="text"> <br> <table>
<tbody><tr> <td> <input name="MainTemplateInheritCheckbox" class="input inheritCheckbox" value="1" checked="checked" type="checkbox"> </td> <td style="font-size: 0.9em;"> <label for="MainTemplateInheritCheckbox">hodnoda sa zdedí z vy??ej úrovne</label> </td> </tr>
</tbody></table>
<br>
</td>
</tr>
<tr>
<td style="vertical-align: top;">
<label for="Lector" class="">?kolite?:</label>
</td>
<td>
<input name="Lector" class="input lector" value="" style="width: 10em;" type="text"> <br> <table> <tbody><tr> <td> <input name="LectorInheritCheckbox" class="input inheritCheckbox" value="1" checked="checked" type="checkbox"> </td> <td style="font-size: 0.9em;"> <label for="LectorInheritCheckbox">hodnoda sa zdedí z vy??ej úrovne</label>
</td> </tr> </tbody></table>
<br>
</td>
</tr>
</table>
我的jQuery代码:
$("input[type=text]").each(function() {
// here I need to find out whether the checkbox is checked or not
});
我试过了,但即使选中了复选框,它也会返回false:
alert($(this).next().next().children("input[type=checkbox]:first").is(":checked"));
1楼
tvanfosson
3
已采纳
2011-03-30 12:06:01
尝试$(this).next('table').find('input[type=checkbox]:first')is(':checked');
您可能还可以通过名称获得它,因为它们是相关的。
var cbName = '#' + $(this).attr('name') + 'InheritCheckbox';
$(cbName).is(':checked');
这两个都有些脆弱,并且取决于DOM中或元素名称之间的关系,尽管我怀疑后者随着时间的推移可能会更加稳定。
2楼
Chandu
2
2011-03-30 12:07:39
试试这个(获取包含表的行并搜索该元素内的复选框):
$("input[type=text]").each(function() {
alert($(this).closest("tr").find("input[type=checkbox]:first").is(":checked"));
});
3楼
Can't Tell
1
2011-03-30 12:20:32
尝试
$("input[type=checkbox]:first",$(this).parent()).is(":checked")
4楼
Alan Kuras
1
2011-03-30 12:22:34
$("input[type=text]").each(function()
{
alert($(this).closest("td").find("input[type=checkbox]").attr("checked"));
});