$("tr.clickable").each(function() {$(this).click(function() {
$(this).children("td:first > input").is(":checked") ?
$(this).children("td:first > input").removeAttr("checked") :
$(this).children("td:first > input").attr("checked","checked");
})});
这仅检查表中的第一行(无论我单击哪一行)。如何应用它以便它检查我单击的特定行上的输入?
HTML:
<tbody style="height: 500px; overflow-x: hidden; overflow-y: scroll;">
<tr id="row0" class="alternate clickable">
<td> <img width="11" height="11" title="This email failed to send." alt="Failed" src="images/error_warning.png">
<br>
<input type="checkbox" value="6751" name="emailCheck[]" class="emailCheck"></td>
<td><a href="user_settings.php?id=349">
wmiller </a></td>
<td>New Fin-iQ Message</td>
<td><span class="errorText">UNKNOWN</span></td>
<td>New Bulletin Notification</td>
<td>Jun. 07/10 10:14:39 am</td>
<td><a href="emails_report.php?action=re&id=6751">Resend</a>
<br>
<a id="emailBodyToggle_6751" href="javascript:showEmail(6751)">Show</a>
</td>
</tr>
<tr id="emailBody_6751" style="display: none; text-align: left;" class="alternate">
<td colspan="7"><div>...</div></td>
</tr>
<tr id="row1" class=" clickable">
<td> <img width="11" height="11" title="This email failed to send." alt="Failed" src="images/error_warning.png">
<br>
<input type="checkbox" value="6752" name="emailCheck[]" class="emailCheck"></td>
<td><a href="user_settings.php?id=350">
mholman </a></td>
<td>New Fin-iQ Message</td>
<td><span class="errorText">UNKNOWN</span></td>
<td>New Bulletin Notification</td>
<td>Jun. 07/10 10:14:39 am</td>
<td><a href="emails_report.php?action=re&id=6752">Resend</a>
<br>
<a id="emailBodyToggle_6752" href="javascript:showEmail(6752)">Show</a>
</td>
</tr>
<tr id="emailBody_6752" style="display: none; text-align: left;" class="">
<td colspan="7"><div>...</div></td>
</tr>
最佳答案
尝试在第一个选择器上删除对 .each() 的调用。 jQuery 使用“隐式迭代”,这意味着您可以执行以下操作:
$('tr.clickable').click(function () { // Do things here. });
这样,您最终应该在每一行上得到一个点击函数。
在弄乱你在 fiddle 上的帖子后,我让它工作:
$("tr.clickable").click(function() {
$(this).find("td:first input").is(":checked") ? $(this).find("td:first input").removeAttr("checked") : $(this).find("td:first input").attr("checked","checked");
});
关于jquery - 通过子项操作来选中表行中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2991734/