jQuery 更改checkbox的狀態,無效

來源:互聯網
上載者:User

標籤:

  今天寫頁面遇到複選框動態全選或全不選問題,正常寫法如下:

$("#tb").find("input[type=‘checkbox‘]").attr("checked","checked");

but!第一次點擊全選按鈕input顯示對勾,第二次就不行了,查了下有建議用prop的,親測有效。那兩者有啥區別呢?

jQuery函數attr()和prop()的區別:1、操作對象不同

  attr和prop分別是單詞attribute和property的縮寫,並且它們均表示"屬性"的意思。

不過,在jQuery中,attribute和property卻是兩個不同的概念。attribute表示HTML文檔節點的屬性,property表示JS對象的屬性。

<!-- 這裡的id、class、data_id均是該元素文檔節點的attribute --><div id="message" class="test" data_id="123"></div><script type="text/javascript">// 這裡的name、age、url均是obj的propertyvar obj = { name: "CodePlayer", age: 18, url: "http://www.365mini.com/" };</script>

  在jQuery中,prop()函數的設計目標是用於設定或擷取指定DOM元素(指的是JS對象,Element類型)上的屬性(property);attr()函數的設計目標是用於設定或擷取指定DOM元素所對應的文檔節點上的屬性(attribute)。

  在jQuery的底層實現中,函數attr()和prop()的功能都是通過JS原生的Element對象(如上述代碼中的msg)實現的。attr()函數主要依賴的是Element對象的getAttribute()和setAttribute()兩個方法。prop()函數主要依賴的則是JS中原生的對象屬性擷取和設定方式。

<div id="message" class="test" data_id="123"></div><script type="text/javascript">var msg = document.getElementById("message");var $msg = $(msg);/* *** attr()依賴的是Element對象的element.getAttribute( attribute ) 和 element.setAttribute( attribute, value ) *** */

// 相當於 msg.setAttribute("data_id", 145);$msg.attr("data_id", 145);// 相當於 msg.getAttribute("data_id");var dataId = $msg.attr("data_id"); // 145/* *** prop()依賴的是JS原生的 element[property] 和 element[property] = value; *** */// 相當於 msg["pid"] = "pid值";$msg.prop("pid", "pid值");// 相當於 msg["pid"];var testProp = $msg.prop("pid"); // pid值</script>

 

  當然,jQuery對這些操作方式進行了封裝,使我們操作起來更加方便(比如以對象形式同時設定多個屬性),並且實現了跨瀏覽器安全色。

此外,雖然prop()針對的是DOM元素的property,而不是元素節點的attribute。不過DOM元素某些屬性的更改也會影響到元素節點上對應的屬性。例如,property的id對應attribute的id,property的className對應attribute的class。

<div id="message" class="test" data_id="123"></div><script type="text/javascript">var msg = document.getElementById("message");var $msg = $(msg);document.writeln( $msg.attr("class") ); // test$msg.prop("className", "newTest");// 修改className(property)導致class(attitude)也隨之更改document.writeln( $msg.attr("class") ); // newTest</script>
2、應用版本不同

  attr()是jQuery 1.0版本就有的函數,prop()是jQuery 1.6版本新增的函數。毫無疑問,在1.6之前,你只能使用attr()函數;1.6及以後版本,你可以根據實際需要選擇對應的函數。

3、用於設定的屬性值類型不同

  由於attr()函數操作的是文檔節點的屬性,因此設定的屬性值只能是字串類型,如果不是字串類型,也會調用其toString()方法,將其轉為字串類型。

prop()函數操作的是JS對象的屬性,因此設定的屬性值可以為包括數組和對象在內的任意類型。

4、其他細節問題

  在jQuery 1.6之前,只有attr()函數可用,該函數不僅承擔了attribute的設定和擷取工作,還同時承擔了property的設定和擷取工作。例如:在jQuery 1.6之前,attr()也可以設定或擷取tagName、className、nodeName、nodeType等DOM元素的property。

  直到jQuery 1.6新增prop()函數,並用來承擔property的設定或擷取工作之後,attr()才只用來負責attribute的設定和擷取工作。

  此外,對於表單元素的checked、selected、disabled等屬性,在jQuery 1.6之前,attr()擷取這些屬性的傳回值為Boolean類型:如果被選中(或禁用)就返回true,否則返回false。

  但是從1.6開始,使用attr()擷取這些屬性的傳回值為String類型,如果被選中(或禁用)就返回checked、selected或disabled,否則(即元素節點沒有該屬性)返回undefined。並且,在某些版本中,這些屬性值表示文檔載入時的初始狀態值,即使之後更改了這些元素的選中(或禁用)狀態,對應的屬性值也不會發生改變。

  因為jQuery認為:attribute的checked、selected、disabled就是表示該屬性初始狀態的值,property的checked、selected、disabled才表示該屬性即時狀態的值(值為true或false)。

  因此,在jQuery 1.6及以後版本中,請使用prop()函數來設定或擷取checked、selected、disabled等屬性。對於其它能夠用prop()實現的操作,也盡量使用prop()函數。

<input id="uid" type="checkbox" checked="checked" value="1"><script type="text/javascript">// 當前jQuery版本為1.11.1var uid = document.getElementById("uid");var $uid = $(uid);document.writeln( $uid.attr("checked") ); // checkeddocument.writeln( $uid.prop("checked") ); // true// 取消複選框uid的選中(將其設為false即可)// 相當於 uid.checked = false;$uid.prop("checked", false);// attr()擷取的是初始狀態的值,即使取消了選中,也不會改變document.writeln( $uid.attr("checked") ); // checked// prop()擷取的值已經發生變化document.writeln( $uid.prop("checked") ); // false</script>

——非原創

jQuery 更改checkbox的狀態,無效

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.