jquery-1.10.2 获取checkbox的checked属性总是undefined
2014-01-13 21:44
344 查看
项目中用的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
$('#regSubmit').click(function(){
var val = $("#reg-checkbox").prop("checked");
console.log(val);
if(val =="checked" ||val==true){
var useremail = $("#reg-email").val();
var password = $("#reg-password").val();
var psdHash = HASH.md5(password);
AUTH.fnReg(useremail,psdHash,function(json){
$("#login-email").attr("value",useremail);
});
}
else{
alert("亲,请阅读并同意 《中科甲骨云服务条款》!");
}
});
相关链接可见: http://my.oschina.net/cxz001/blog/111730
/article/8438390.html
查找资料,特此记录:
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
$('#regSubmit').click(function(){
var val = $("#reg-checkbox").prop("checked");
console.log(val);
if(val =="checked" ||val==true){
var useremail = $("#reg-email").val();
var password = $("#reg-password").val();
var psdHash = HASH.md5(password);
AUTH.fnReg(useremail,psdHash,function(json){
$("#login-email").attr("value",useremail);
});
}
else{
alert("亲,请阅读并同意 《中科甲骨云服务条款》!");
}
});
相关链接可见: http://my.oschina.net/cxz001/blog/111730
/article/8438390.html
相关文章推荐
- jQuery 1.9 移除了 $.browser 的替代方法
- jQuery 1.9 移除了 $.browser 的替代方法
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- 锋利的jquery---第三章(超链接提示)
- 锋利jquery----3-10设置和获取HTML,文本和值
- JQUERY1.9学习笔记 之基本过滤器(一) 动态选择器
- JQuery fn fy
- JQUERY1.9学习笔记 之层级选择器(四)
- Django & JQuery 实现 Autocomplete
- 使用 Jquery 选中获取 checkbox radio 和 select的 value 和 text
- JQuery each详细用法
- 一个比较好用的轻量级时间选择控件 ----- JQUERY特效模板
- Eclipse下jQuery文件报错出现错误提示红叉
- 实践jquery 高级编程(续集一)
- js jquery分别实现动态的文件上传操作按钮的添加和删除
- jQuery文本段落展开和折叠效果
- 查询条件清空
- 开源jQuery插件推荐,帮你快速构建单页网站
- Jquery对表格进行操作——添加行、删除行和绑定行
- 如何忘却jQuery,开始使用JavaScript原生API