How to make this JS code to w开发者_StackOverflow社区ork with all the checkboxes within a td id="sel";
$(document).ready(function() {
$('input[name="selector"]').click(function() {
$("#sel :checkbox").attr('checked', $(this).is(':checked'));
});
});
Try this
$(document).ready(function() {
$('input[name="selector"]').click(function() {
$("#sel input:checkbox").attr('checked', $(this).is(':checked'));
});
});
Ah okay, that makes sense... so it's like a 'Select All' selector? Here's an example...
//Attach to our select all checkbox
$("input:checkbox[name='SelectAll']").click(function() {
var checked = $(this).is(":checked");
//Skip up to the nearest common container that holds your
//SelectAll and SelectItem checkboxes... i.e. table
var container = $(this).closest("table");
container.find("input:checkbox[name='Selector']").attr('checked', checked);
});
The same thing would be used for all checkboxes within the sel that has id "sel"...
//Attach to our select all checkbox
$("input:checkbox[name='SelectAll']").click(function() {
var checked = $(this).is(":checked");
$("td#sel input:checkbox").attr("checked", checked);
});
hint: you can only have one cell with id: "sel"... if you have more than one, only the first cell will be recognized with that id, if you want multiple cells to have an "id" of the same name, you must use the "name" attribute instead of the "id" attribute:
Change your td definintion to <td name="sel" />
and use the selector
$("td[name='sel'] input:checkbox").attr("checked", checked);
Now you can have multiple cells with the "sel" selector and all your checkboxes will check...
Here's my final solution; Thanks everybody!
$(document).ready (function () {
$('input[id="selector"]').click (function () {
var arr = [ {foreach from = $membri item = id} "{$id.id}", {/foreach} ];
jQuery.each (arr, function () {
$("#sel" +this+ ":checkbox").attr ('checked', $("#selector").is(':checked') );
} );
} );
} );
精彩评论