jQuery .attr("checked&…
2015-03-19 14:37
399 查看
今天在写“记住密码”部分是,用到jQuery判断复选框是否选中:
function remember(){
if($("#remember").attr("checked")==true){
var username = $("#username").val();
var password = $("#password").val();
//alert("添加cookies"+username+password);
$.cookie('username',username,{expires:10});
$.cookie('password',password,{expires:10});
$.cookie('rememberUser','true',{expires:10});
}
}
但是得到$("#remember").attr("checked")为undefined,于是上网找各种方法,后来看见一文说改成prop就行了,果然。
文中还提到关于attr的用法注意事项:
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()方法。
那么,什么时候使用attr(),什么时候使用prop()?
1.添加属性名称该属性就会生效应该使用prop();
2.是有true,false两个属性使用prop();
3.其他则使用attr();
项目中jquery升级的时候大家要注意这点!
以下是官方建议attr(),prop()的使用:
function remember(){
if($("#remember").attr("checked")==true){
var username = $("#username").val();
var password = $("#password").val();
//alert("添加cookies"+username+password);
$.cookie('username',username,{expires:10});
$.cookie('password',password,{expires:10});
$.cookie('rememberUser','true',{expires:10});
}
}
但是得到$("#remember").attr("checked")为undefined,于是上网找各种方法,后来看见一文说改成prop就行了,果然。
文中还提到关于attr的用法注意事项:
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()方法。
那么,什么时候使用attr(),什么时候使用prop()?
1.添加属性名称该属性就会生效应该使用prop();
2.是有true,false两个属性使用prop();
3.其他则使用attr();
项目中jquery升级的时候大家要注意这点!
以下是官方建议attr(),prop()的使用:
Attribute/Property | .attr() | .prop() |
---|---|---|
accesskey | √ | |
align | √ | |
async | √ | √ |
autofocus | √ | √ |
checked | √ | √ |
class | √ | |
contenteditable | √ | |
draggable | √ | |
href | √ | |
id | √ | |
label | √ | |
location ( i.e. window.location ) | √ | √ |
multiple | √ | √ |
readOnly | √ | √ |
rel | √ | |
selected | √ | √ |
src | √ | |
tabindex | √ | |
title | √ | |
type | √ | |
width ( if needed over .width()) | √ |
相关文章推荐
- jquery学习2---.attr("checked")?
- Jquery 版本兼容性 attr("checked") 返回undefined或失效
- jQuery.attr("checked")不能判断复选框是否被选中
- Jquery 1.6+ 以上 attr("checked") 返回的不是 bool 类型!!!
- Jquery 版本兼容 attr("checked") 返回undefined 获取选中失效
- jQuery获取checkbox状态时attr('checked')返回undefined的问题
- jquery checkbox反复调用attr('checked', true/false)只有第一次生效
- jQuery attr("value") 和 val的区别
- Jquery使用attr("checked")获取的值为undefined
- jquery 1.11 attr("disabled" 无效
- Jquery attr("checked") 返回checked或undefined 获取选中失效
- jquery 1.6 后 checkbox 使用attr("checked"),undefined.
- checkbox的attr("checked")一直以来,undefined问题解决
- Jquery attr("checked") 返回checked或undefined 获取选中失效
- IE7下不支持jquery.attr("onclick","function(){}"),
- jquery版本升级不兼容的问题:$("input").attr("value")功能发生改变
- jquery attr()方法
- Jquery 版本兼容性 attr("checked") 返回undefined或失效
- IE7下不支持jquery.attr("onclick","function(){}"),
- Jquery使用attr("checked")返回checked或undefined导致获取是否选中失效