jquery-1.10.2 获取checkbox的checked属性总是undefined
2014-12-10 11:44
495 查看
项目中用的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("亲,请阅读并同意 《中科甲骨云服务条款》!");
}
});
查找资料,特此记录:
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("亲,请阅读并同意 《中科甲骨云服务条款》!");
}
});
相关文章推荐
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery-1.10.2 获取checkbox的checked属性总是undefined
- jquery 获取checkbox的checked属性总是undefined
- jquery 1.6+ 获取checkbox的checked属性总是undefined
- jquery1.6+ 获取checkbox的checked属性总是undefined
- jquery 1.6+ 获取checkbox的checked属性总是checked
- jQuery获取的checkbox的属性checked为undefined
- jquery 1.6+ 获取checkbox的checked属性总是checked
- Jquery获取checkbox属性checked为undefined
- Jquery获取checkbox属性checked为undefined
- jQuery获取和设置checkbox的checked属性小结
- jQuery获取checkbox状态时attr('checked')返回undefined的问题
- jquery获取checkbox属性为undefined解析
- Jquery 获取checkbox的checked问题以及解决方案
- jquery 获取不到checkbox checked时
- Jquery attr("checked") 返回checked或undefined 获取选中失效