• jquery-1.10.2 获取checkbox的checked属性总是undefined


     项目中用的jquery-1.10.2 需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined 未定义。

           查找资料,特此记录:

           jq官网说明:

          As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop()method.

           .attr() 不能用于普通对象,数组,窗口,文档等,要重新获取改变dom属性,用.prop()方法。

            用.prop方法确实可行。

            贴一段代码如下:

           

    [html] view plaincopy
    1. $('#regSubmit').click(function(){  
    2.            
    3.             var val = $("#reg-checkbox").prop("checked");  
    4.    
    5.             console.log(val);  
    6.               
    7.             if(val =="checked" ||val==true){    
    8.                   
    9.                 var useremail = $("#reg-email").val();  
    10.                 var password = $("#reg-password").val();  
    11.                   
    12.                 var psdHash = HASH.md5(password);  
    13.           
    14.                 AUTH.fnReg(useremail,psdHash,function(json){  
    15.                       
    16.                     $("#login-email").attr("value",useremail);  
    17.                 });  
    18.             }  
    19.             else{  
    20.                 alert("亲,请阅读并同意 《中科甲骨云服务条款》!");  
    21.             }  
    22.      });  


            相关链接可见: http://my.oschina.net/cxz001/blog/111730 





  • 相关阅读:
    仿网易/QQ空间视频列表滚动连播炫酷效果
    UIScrollerView当前显示3张图
    iOS AVPlayer视频播放器
    iOS 购物车动画
    C++从零实现简单深度神经网络(基于OpenCV)
    Android 轻松实现仿淘宝地区选择
    微信小程序之下拉刷新,上拉更多列表实现
    iOS 指南针
    tableView 获取网络图片,并且设置为圆角(优化,fps)
    黑客技术 —— Linux 命令行
  • 原文地址:https://www.cnblogs.com/y0umer/p/3838936.html
Copyright © 2020-2023  润新知