欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery無法設(shè)置checkbox選中即沒有變成選中狀態(tài)

 更新時間:2014年03月27日 11:46:20   作者:  
設(shè)置以后checkbox并沒有變成選中狀態(tài),用chrome調(diào)試看了一下,checkbox中確實有checked屬性,針對這個問題,大家可以參考下本文
復(fù)制代碼 代碼如下:

$("input").attr("checked","checked")

設(shè)置以后checkbox并沒有變成選中狀態(tài),用chrome調(diào)試看了一下,checkbox中確實有checked屬性,而且,值為checked,但是頁面顯示仍然為未選中狀態(tài)
復(fù)制代碼 代碼如下:

$("input").prop("checked",true);


ttributes和properties之間的差異在特定情況下是很重要。jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值時,會返回 property 的值,這就導(dǎo)致了結(jié)果的不一致。從 jQuery 1.6 開始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。

例如, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected 應(yīng)使用.prop()方法進行取值或賦值。 在jQuery1.6之前,這些屬性使用.attr()方法取得,但是這并不是元素的attr屬性。他們沒有相應(yīng)的屬性(attributes),只有特性(property)。

例如,考慮一個DOM元素的HTML標記中定義的<input type="checkbox" checked="checked" /> ,并假設(shè)它是一個JavaScript變量命名的elem :

elem.checked true (Boolean) 將改變復(fù)選框的狀態(tài)
$(elem).prop("checked") true (Boolean) 將改變復(fù)選框的狀態(tài)
elem.getAttribute("checked") "checked" (String) 不會改變的復(fù)選框的初始狀態(tài);
$(elem).attr("checked") (1.6) "checked" (String) 不會改變的復(fù)選框的初始狀態(tài);
$(elem).attr("checked") (1.6.1+) "checked" (String) 將改變復(fù)選框的狀態(tài)
$(elem).attr("checked") (pre-1.6) true (Boolean) 將改變復(fù)選框的狀態(tài)
根據(jù)W3C的表單規(guī)范 ,在checked屬性是一個布爾屬性,這意味著只要該 attribute 存在,即使它沒有值,或是一個空字符串,該屬性對應(yīng)的 property 就是 true。以下推薦的是兼容瀏覽器方式,判斷 checkbox 元素的 checked 屬性是否為"真" 的方法:
復(fù)制代碼 代碼如下:

if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )

如果你使用jQuery 1.6 ,代碼if ( $(elem).attr("checked") ),將獲得一個屬性(attribute) ,它不改變該復(fù)選框被選中和選中。它只是用來存儲默認或選中屬性的初始值。為了保持向后兼容,.attr() 方法從 jQuery 1.6.1+ 開始除了返回屬性值外,還會更新 property 屬性,因此 boolean attribute(布爾屬性)不需要通過 .prop() 來改變其值。推薦使用上述方法之一,來取得 checked 的值。

相關(guān)文章

最新評論