update 2013.09.22: jQuery 1.6 後請用 prop 取代 attr
update 2009.05.13: jQuery 1.3 版之後不支援
$("input[@name='user_active_col[]']")
請改寫成
$("input[name='user_active_col[]']")
在 jQuery 底下要如何實現這個功能,其實還蠻簡單的,首先看 html 部份
<
input name=
"user_active_col[]" type=
"checkbox" value=
"1"> 1
<
input name=
"user_active_col[]" type=
"checkbox" value=
"2"> 2
<
input name=
"user_active_col[]" type=
"checkbox" value=
"3"> 3
<
input name=
"user_active_col[]" type=
"checkbox" value=
"4"> 4
<
input name=
"user_active_col[]" type=
"checkbox" value=
"5"> 5
<
input name=
"clickAll" id=
"clickAll" type=
"checkbox"> 全選
jQuery 部份如下:
$("#clickAll").click(function() {
if($("#clickAll").prop("checked"))
{
$("input[name='user_active_col[]']").each(function() {
$(this).prop("checked", true);
});
}
else
{
$("input[name='user_active_col[]']").each(function() {
$(this).prop("checked", false);
});
}
});