Jquery 獲取Checkbox值,prop 和 attr 函數區別


總結:

版本 1.6 1.6 1.4 1.4
函數 勾選 取消勾選 勾選 取消勾選
attr('checked')
checked undefined true false
.prop('checked')
true false   1.6才有此方法
.is(':checked')
true false true false

elem.checked
true (Boolean) Will change with checkbox state

$(elem).prop("checked")
true (Boolean) Will change with checkbox state

elem.getAttribute("checked")
"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6)
"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6.1+)
"checked" (String) Will change with checkbox state

$(elem).attr("checked")(pre-1.6)
true (Boolean) Changed with checkbox state

 

 

今天在用JQuery的時候發現一個問題用.attr("checked")獲取checkbox的checked屬性時選中的時候可以取到值,值為"checked"但沒選中獲取值就是undefined.

解決這個文章我參考了這個帖子:

http://bugs.jquery.com/ticket/9812

為什么jquery 1.6+增加了.prop()方法,因為在有些瀏覽器中比如說只要寫disabled,checked就可以了,而有的要寫成disabled = "disabled",checked="checked"。所以,從1.6開始,jq提供新的方法“prop”來獲取這些屬性。
以前我們使用attr獲取checked屬性時返回"checked"和"",現在使用prop方法獲取屬性則統一返回true和false。
那么,什么時候使用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() )

 

下文來自www.jquery.com The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop()method provides a way to explicitly retrieve property values, while .attr() retrieves attributes For example, selectedIndex, tagName, nodeName, nodeType,ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

elem.checked
true (Boolean) Will change with checkbox state

$(elem).prop("checked")
true (Boolean) Will change with checkbox state

elem.getAttribute("checked")
"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6)
"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6.1+)
"checked" (String) Will change with checkbox state

$(elem).attr("checked")(pre-1.6)
true (Boolean) Changed with checkbox state

原文:http://www.cnblogs.com/-run/archive/2011/11/16/2251569.html
1.6版情況:
//勾選后輸出:
//attr('checked'): checked
//.prop('checked'): true
//.is(':checked'): true

//取消勾選輸出:

//.attr('checked'): undefined
//.prop('checked'): false
//.is(':checked'): false


jquery1.4 版本:

復制代碼

 1 <!DOCTYPE html> 
2 <html>
3 <head>
4 <style>
5 p { margin: 20px 0 0 }
6 b { color: blue; }
7 </style>
8 <script src="../js/jquery-1.4.4.js"></script>
9 </head>
10 <body>
11
12 <input id="check1" type="checkbox" checked="checked">
13 <label for="check1">Check me</label>
14 <p></p>
15
16 <script>
17 $("input").change(function() {
18 var $input = $(this);
19 $("p").html(".attr('checked'): <b>" + $input.attr('checked') + "</b><br>"
20 + ".is(':checked'): <b>" + $input.is(':checked') ) + "</b>";
21 }).change();
22 </script>
23
24 </body>
25 </html>

復制代碼

勾選后輸出:
//attr('checked'): true
//.prop('checked') 1.6后版本才有這個方法
//.is(':checked'): true

取消勾選輸出:



//.attr('checked'): false
//.prop('checked')1.6后版本才有這個方法
//.is(':checked'): false



結論: attr('checked'): 在1.6后版本,所獲取的值是 "checked"/"underfined"  ,之前所獲得的值是"false"/"true"。截然不同


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM