• jquery操作复选框(checkbox)的一些小技巧总结


    1、获取单个checkbox选中项(三种写法)

    //第一种
    $("input:checkbox:checked").val()
    //第二种
    $("input:[type='checkbox']:checked").val();
    //第三种
    $("input:[name='ck']:checked").val();

    2、 获取多个checkbox选中项

    $('input:checkbox').each(function() {
            if ($(this).attr('checked') ==true) {
                    alert($(this).val());
            }
    });

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

    //第一种
    $('input:checkbox:first').attr("checked",'checked');
    //第二种
    $('input:checkbox').eq(0).attr("checked",'true');

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

    //第一种
    $('input:radio:last').attr('checked', 'checked');
    //第二种
    $('input:radio:last').attr('checked', 'true');

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

    //第一种
    $('input:checkbox).eq(索引值).attr('checked', 'true');索引值=0,1,2....
    //第二种
    $('input:radio').slice(1,2).attr('checked', 'true');

    6、选中多个checkbox同时选中第1个和第2个的checkbox

    $('input:radio').slice(0,2).attr('checked','true');

    7、根据Value值设置checkbox为选中值

    $("input:checkbox[value='1']").attr('checked','true');

    8、删除Value=1的checkbox

    $("input:checkbox[value='1']").remove();

    9、删除第几个checkbox

    $("input:checkbox").eq(索引值).remove();//索引值=0,1,2....
    //如删除第3个checkbox:
    $("input:checkbox").eq(2).remove();

    10、遍历checkbox

    $('input:checkbox').each(function (index, domEle) {
    //写入代码
    });

    11、全部选中

    $('input:checkbox').each(function() {
            $(this).attr('checked', true);
    });

    12、全部取消选择

    $('input:checkbox').each(function () {
            $(this).attr('checked',false);
    });

    13、checkbox标签已有checked=checked但是不显示勾选

    checkbox标签已有checked=checked但是不显示勾选,解决办法就是将   $("...").attr("checked", true)改为$("...").prop("checked", true)

      JQuey中的 attr()函数和prop()函数,attr()是处理 attribute的值的,而prop()是处理 property 的值的 ,jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值,那么归根结底,就是在处理 attribute 和 property。
            很多attribute节点有一个相应的property属性,因而attribute和property很容易被混淆在一起,如某个div元素中的id和class既是attribute也有property,不管哪种方式都可以访问和修改,但是对于自定义的attribute节点,或者自定义property,两者就没有关系了(但是在IE6-7中,两者还是一样的,但愿我们都能早日抛弃IE8以下的),需要注意的是,对于checked特性(attribute)不是对应它checked属性(property),attribute实际对应的是defaultChecked属性,而且仅用于设置复选框最初的值,checked的attribute不会因为复选框的状态而改变,而checked的property会因为复选框的状态而改变,所以在.attr()函数中,就算设置成了.attr("checked", true),也只是用来存储默认或者选中属性的默认值,却并不改变该复选框被选中和选中,这就是为什么   checkbox标签已有checked=checked但是不显示勾选 的原因所在。

    //.attr('checked');看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false
    //.prop('checked'); //16+:true/false
    //.is(':checked'); //所有版本:true/false//别忘记冒号

  • 相关阅读:
    java虚拟机理解探索1
    Java线程面试题 Top 50(转载)
    (转载)浅谈我对DDD领域驱动设计的理解
    最大堆
    利用筛法求质数
    递归算法及优化
    java 根据传入的时间获取当前月的第一天的0点0分0秒和最后一天的23点59分59秒
    Uncaught Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 3
    mysql 子查询问题
    微信7.0以后更新后H5页面定位不准确
  • 原文地址:https://www.cnblogs.com/dyhao/p/9213750.html
Copyright © 2020-2023  润新知