首页 技术 正文
技术 2022年11月13日
0 收藏 740 点赞 4,315 浏览 734 个字

今天在查看他人源码时看到在判断复选框是否选中时,与自己的写法不同:

   .is(“:checked”) vs .prop(“checked”) == true

  因此,特地百度了一下,结果如下:

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

  jquery赋值checked的几种写法:

  所有的jquery版本都可以这样赋值:

  // $(“#cb1″).attr(“checked”,”checked”);
  // $(“#cb1″).attr(“checked”,true);

  jquery1.6+:prop的4种赋值:

  // $(“#cb1″).prop(“checked”,true);//很简单就不说了哦
  // $(“#cb1″).prop({checked:true}); //map键值对
  // $(“#cb1″).prop(“checked”,function(){
    return true;//函数返回true或false
  });

  //$(“#cb1″).prop(“checked”,”checked”);

综上所述:在判断复选框、单选框等有checked属性的元素是否被选中时,兼容最好的是:

  .is(‘:checked’):    //所有版本:true/false//别忘记冒号哦

参考网址:http://www.wufangbo.com/jquery-pan-duan-checked/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898