代码如下:
function CheckAll(val) {
$("input[name='chkJob']").each(function() {
this.checked = val;
});
$("#chkAll").attr("checked", val);//设定全选按钮状态
}
val 该参数传递的是全选按钮的选择状态
name='chkJob' 这个为列表中的checkbox名称
chkAll 就为全选按钮名称
喜欢上了Jquery的简便、明了···
与用JavaScript写的比起来真的有点差距!
eg:
javascript教程 全选反选代码
复制代码 代码如下:
//全选
function checkall() {
var all = document.getElementsByTagName("input");
for (var i = 0; i < all.length; i++) {
if (all[i].type == "checkbox") {
all[i].checked = true;
}
}
}
//反选
function checknull() {
var all = document.getElementsByTagName("input");
for (var i = 0; i < all.length; i++) {
if (all[i].type == "checkbox") {
all[i].checked = false;
}
}
}
方法二
(function($){
$.fn.checkgroup = function(options){
//merge settings
settings=$.extend({
groupSelector:null,
groupName:'group_name',
enabledOnly:false
},options || {});var ctrl_box=this;
//allow a group selector override option
var grp_slctr = (settings.groupSelector==null) ? 'input[name='+settings.groupName+']' : settings.groupSelector;//grab only enabled checkboxes if required
if(settings.enabledOnly)
{
grp_slctr += ':enabled';
}//attach click event to the "check all" checkbox(s)
ctrl_box.click(function(e){
chk_val=(e.target.checked);
$(grp_slctr).attr('checked',chk_val);
//if there are other "select all" boxes, sync them
ctrl_box.attr('checked',chk_val);
});
//attach click event to checkboxes in the "group"
$(grp_slctr).click(function(){
if(!this.checked)
{
ctrl_box.attr('checked',false);
}
else
{
//if # of chkbxes is equal to # of chkbxes that are checked
if($(grp_slctr).size()==$(grp_slctr+':checked').size()){
ctrl_box.attr('checked','checked');
}
}
});
//make this function chainable within jquery
return this;
};
})(jQuery);<input type='checkbox' class='checkall'>checkall<br>
<input class='groupclass' name='group' type='checkbox'>chk1<br>
<input class='groupclass' name='group' type='checkbox'>chk2<br>
<input class='groupclass' name='group' type='checkbox'>chk3<br>
<input class='groupclass' name='group' type='checkbox'>chk4<br>
<input type='checkbox' class='checkall' id="checkall">
<?php教程
$(function() {
$("#checkall").click(function() {
$('.checkall').checkgroup({groupSelector:'.groupclass',enabledOnly:true});
});
});
首页 1 2 末页