admin管理员组文章数量:1389754
I've got a checkbox inside a table, and when you click on it, it'll change the background colour accordingly like so...
$("#table tr td :checkbox").bind("click change", function() {
$this = $(this); // cache the jquery object
if($this.is(':checked')) { $this.closest('tr').css('background-color', 'lightyellow'); }
else { $this.closest('tr').css('background-color', '#fff'); }
});
That works just fine, however, I figured I'd like to go one better, so anywhere on the table row you click, it'll check the box and highlight the row.
I tried using this code, but it doesn't work unfortunately:
$("table tr").bind("click", function() {
$(this).parents("tr").find(":checkbox").attr('checked');
});
And here's the HTML code (removed excessive stuff to improve readability...
<td>Name</td>
<td>Description</td>
<td><input type="checkbox"></td>
Any help would be very much appreciated, thank you!
I've got a checkbox inside a table, and when you click on it, it'll change the background colour accordingly like so...
$("#table tr td :checkbox").bind("click change", function() {
$this = $(this); // cache the jquery object
if($this.is(':checked')) { $this.closest('tr').css('background-color', 'lightyellow'); }
else { $this.closest('tr').css('background-color', '#fff'); }
});
That works just fine, however, I figured I'd like to go one better, so anywhere on the table row you click, it'll check the box and highlight the row.
I tried using this code, but it doesn't work unfortunately:
$("table tr").bind("click", function() {
$(this).parents("tr").find(":checkbox").attr('checked');
});
And here's the HTML code (removed excessive stuff to improve readability...
<td>Name</td>
<td>Description</td>
<td><input type="checkbox"></td>
Any help would be very much appreciated, thank you!
Share Improve this question edited May 10, 2023 at 9:12 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Aug 14, 2009 at 12:55 NickNick 1,24313 gold badges26 silver badges37 bronze badges3 Answers
Reset to default 2The event your handling is the tr click. The parent is the table so that will not help. All you need to do is use find() from the this context.
I would use .live to avoid multiple event handlers when one will do.
Assuming you only have one checkbox in the row then use the following. (note it uses tr's inside tbody to avoid running this on thead rows)
$("table>tbody>tr").live("click", function() {
$(this).find(":checkbox").attr('checked', 'checked');
});
UPDATE
If you want to toggle it try something like
$("table>tbody>tr").live("click", function(ev) {
var $checkbox = $(this).find(":checkbox");
//check to see we have not just clicked the actual checkbox
if ( !$(ev.target).is(':checkbox') ){
$checkbox.is(':checked') ? $checkbox.removeAttr('checked')
: $checkbox.attr('checked', 'checked')
}
});
You want to change this:
$(this).parents("tr").find(":checkbox").attr('checked');
to this:
$(this).parents("tr").find(":checkbox").attr('checked', 'checked');
Otherwise all you're doing is reading the checked
attribute, not setting it.
I think you are just forgetting to set the value of the attribute.
$("table tr").bind("click", function() {
$(this).find(":checkbox").attr('checked', 'checked');
});
The jQuery Docs on Attributes might be of some assistance.
Credit to redsquare for noticing that the .parent("tr")
is not needed.
本文标签: javascriptFinding the next input (of a parent) with jQueryStack Overflow
版权声明:本文标题:javascript - Finding the next input (of a parent?) with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744571764a2613382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论