明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

jQuery操作复选框checkbox方法

Posted on   且行且思  阅读(3510)  评论(0编辑  收藏  举报

一、jQuery判断checked是否是选中状态的三种方法:

$("#cb1").attr('checked') // 返回:"checked"或"undefined";
$("#cb1").prop('checked') // 返回true/false
$("#cb1").is(':checked')  // 返回true/false //别忘记冒号哦

 

二、jQuery赋值checked的几种写法: 所有的jQuery版本都可以这样赋值,不建议用attr();

复制代码
$("#cb1").attr("checked","checked"); //通用做法,现在不推荐
$("#cb1").attr("checked",true); //不标准,不推荐
$("#cb1").attr("checked","true"); //不标准,不推荐

//jQuery的prop()的4种赋值(推荐如下写法):
$("#cb1").prop("checked",true); //标准写法,推荐!
$("#cb1").prop({checked:true}); //map键值对    
$("#cb1").prop("checked",function(){
  return true;//函数返回true或false
});
复制代码

 

三、jQuery操作checkbox技巧总结

1.获取单个checkbox选中项的值(三种写法)

$("#chx1").find("input:checkbox:checked").val()
//或者
$("#chx1").find("input:[type='checkbox']:checked").val();
$("#chx1").find("input[type='checkbox']:checked").val();
//或者
$("#chx1").find("input:[name='ck']:checked").val();
$("#chx1").find("input[name='ck']:checked").val();

2.获取多个checkbox选中项

复制代码
$("#chk1").find('input:checkbox').each(function() { //遍历所有复选框
    if ($(this).prop('checked') == true) {
        console.log($(this).val()); //打印当前选中的复选框的值
    }
});

function getCheckBoxVal(){ //jquery获取所有选中的复选框的值
    var chk_value =[];
    $("#chk1").find('input[name="test"]:checked').each(function(){ //遍历,将所有选中的值放到数组中
        chk_value.push($(this).val());
    });
    alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);
}
复制代码

3.设置第一个checkbox 为选中值

$("#chk1").find('input:checkbox:first').prop("checked",true);
//或者
$("#chk1").find('input:checkbox').eq(0).prop("checked",true);

4.设置最后一个checkbox为选中值

$("#chk1").find('input:checkbox:last').prop("checked",true);

5.根据索引值设置任意一个checkbox为选中值

$("#chk1").find('input:checkbox').eq(索引值).prop('checked', true);  //索引值=0,1,2....
//或者
$("#chk1").find('input:checkbox').slice(1,2).prop('checked', true); //同时选中第0个和第1个checkbox
//从索引0开始(包含),到索引2(不包含)的checkbox

6.根据value值设置checkbox为选中值

$("#chk1").find("input:checkbox[value='1']").prop('checked',true);
$("#chk1").find("input[type='checkbox'][value='1']").prop('checked',true);

 7.删除checkbox:①删除value=1的checkbox ②删除第几个checkbox

$("#chk1").find("input:checkbox[value='1']").remove(); //将匹配元素从DOM中删除,将标签从页面上删除了
$("#chk1").find("input:checkbox").eq(index).remove(); //索引值 index=0,1,2....
//如删除第3个checkbox:
$("#chk1").find("input:checkbox").eq(2).remove();

8.全部选中或全部取消选中

$("#chk1").find('input:checkbox').each(function() {
    $(this).prop('checked', true);
});
//或者
$("#chk1").find('input:checkbox').each(function () {
    $(this).prop('checked',false);
});

 9.选中所有奇数项或偶数项

$("#chk1").find("input[type='checkbox']:even").prop("checked",true); //选中所有偶数
$("#chk1").find("input[type='checkbox']:odd").prop("checked",true); //选中所有奇数

 

四.反选

复制代码
/*方法一:*/
$("#btn4").click(function(){
    $("input[type='checkbox']").each(function(){ //反选
         if($(this).prop("checked")){
            $(this).prop("checked",false);
          } else{
             $(this).prop("checked",true);
         }
    });    
});

/*方法二:*/
$("#btn4").on('click',function(){
//反选所有的复选框(没选中的改为选中,选中的改为取消选中)
    $("#chk1").find("input[type='checkbox']").prop("checked", function(index, oldValue){
        return !oldValue;
    });
}
复制代码

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2022-08-04 MySQL-建表时或修改字段长度时,报:1118 - Row size too large等问题的解决方法
2020-08-04 PostgreSQL中有以下格式化函数:
2017-08-04 asp.net:mv4 FileResult在IE8中下载不显示文件名和扩展名而显示Action方法名了!
点击右上角即可分享
微信分享提示