jquery checkbox checked/unchecked problem

<form>
        What is your hobby sport? <input type="checkbox" id="CheckedAll" />select all/unselect all<br />
        <input type="checkbox" name="items" value="足球" />足球
        <input type="checkbox" name="items" value="篮球" />篮球
        <input type="checkbox" name="items" value="羽毛球" />羽毛球
        <input type="checkbox" name="items" value="table tennis" />table tennis
        <br />
        <input type="button" id="send" value="提 交" />
    </form>


$("#CheckedAll").click(function () { if ($(this).is(":checked")) { $("[name=items]:checkbox").attr("checked", true); } else { $("[name=items]:checkbox").attr("checked", false); } });

The first execution, no problem, but the second execution there is a problem, can not choose

Solution: replace attr() with prop()

 

$("#CheckedAll").click(function () {
                if ($(this).is(":checked")) {
                    $("[name=items]:checkbox").prop("checked", true);
                } else {
                    $("[name=items]:checkbox").prop("checked", false);
                }
            });

 

PS: The difference between prop() and attr():

(Source: http://hxq0506.iteye.com/blog/1046334 )

I recently saw in iteye's news that jQuery has been updated to 1.6.1. The biggest change from previous versions is the addition of the .prop method. But the .prop() method and the .attr() method are literally indistinguishable. In Chinese, properties and attributes both mean "attributes". Below is a brief translation of the usage of .prop() and .attr()
based on this blog post ( javascript:mctmp(0); ):

1. Upgrade from 1.5.2 to 1.6.1

通过介绍新方法.prop()以及.attr()方法的改变,jQuery1.6.1引起了一场关于attributes和properties之间有何区别和联系的激烈讨论。同时,1.6.1也解决了一些向后兼容性问题。当从1.5.2升级到1.6.1时,你不必修改任何attribute代码。

下面是关于jQuery1.6和1.6.1中Attributes模块变化的描述,以及.attr()方法和.prop()方法的首选使用。然而,正如前面所述,jQuery1.6.1允许你使用.attr()方法就像以前它被使用在所有的情况中一样。

 

2、发生了什么变化

Attributes模块的变化是移除了attributes和properties之间模棱两可的东西,但是在jQuery社区中引起了一些混乱,因为在1.6之前的所有版本中都使用一个方法(.attr())来处理attributes和properties。但是老的.attr()方法有一些bug,很难维护。jQuery1.6.1对Attributes模块进行了更新,并且修复了几个bug。

特别提到的是,boolean attributes,比如:checked,selected,readonly和disabled在1.6.1中和1.6之前的处理相同。这意味着下面的代码:

 

  1. $(“:checkbox”).attr(“checked”, true);  
  2. $(“option”).attr(“selected”, true);  
  3. $(“input”).attr(“readonly”, true);  
  4. $(“input”).attr(“disabled”, true);  

 

 甚至是这样的代码:

 

  1. if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }  

 

在1.6.1中没有必要为了保持之前期望的运行结果而发生任何改变。

 

 

为了让jQuery1.6中的.attr()方法的变化被理解的清楚些,下面是一些使用.attr()的例子,虽然在jQuery之前的版本中能正常工作,但是现在必须使用.prop()方法代替:

 

<iframe id="iframe_0.41162012866698205" style="margin: 0px; padding: 0px; border: none; width: 514px; height: 149px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://dl.iteye.com/upload/attachment/482357/1c60909d-26f4-313b-85b9-5e39c480eeff.jpg?_=3799895%22%20style=%22border:none;max-width:1014px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.41162012866698205',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

 

首先,window或document中使用.attr()方法在jQuery1.6中不能正常运行,因为window和document中不能有attributes。它们包含properties(比如:location或readyState),必须使用.prop()方法操作或简单地使用javascript原生的方法。在jQuery1.6.1中,window和document中使用.attr()将被自动转成使用.prop,而不是抛出一个错误。

其次,checked,selected和前面提到的其它boolean attributes,因为这些attributes和其相应的properties之间的特殊关系而被特殊对待。基本上,一个attribute就是以下html中你看到的:

 

  1. <input type=”checkbox” checked=”checked”>  

 

boolean attributes,比如:checked,仅被设置成默认值或初始值。在一个checkbox的元素中,checked attributes在页面加载的时候就被设置,而不管checkbox元素是否被选中。

 

properties就是浏览器用来记录当前值的东西。正常情况下,properties反映它们相应的attributes(如果存在的话)。但这并不是boolean attriubutes的情况。当用户点击一个checkbox元素或选中一个select元素的一个option时,boolean properties保持最新。但相应的boolean attributes是不一样的,正如上面所述,它们仅被浏览器用来保存初始值。

 

  1. $(“:checkbox”).get(0).checked = true;  
  2. // Is the same as $(":checkbox:first").prop(“checked”, true);  

 

在jQuery1.6中,如果使用下面的方法设置checked:

 

  1. $(“:checkbox”).attr(“checked”, true);  

 

将不会检查checkbox元素,因为它是需要被设置的property,但是你所有的设置都是初始值。

 

然而,曾经jQuery1.6被释放出来的时候,jQuery团队明白当浏览器仅关心页面加载时,设置一些值不是特别的有用。所以,为了保持向后兼容性和.attr()方法的有用性,我们可以继续在jQuery1.6.1中使用.attr()方法取得和设置这些boolean attributes。

 

最普通的attributes是checked,selected,disabled和readOnly,但下面是jQuery1.6.1支持的使用.attr()动态地取得和设置boolean attributes/properties的完整列表:

 

  1. autofocus, autoplay, async, checked, controls, defer, disabled,  
  2. hidden, loop, multiple, open, readonly, required, scoped, selected  

 

(译者注:大部分都是html5新增的属性)

 

还是建议使用.prop()方法来设置这些boolean attributes/properties,即使这些用例没有转换成使用.prop()方法,但是你的代码仍然可以在jQuery1.6.1中正常运行。

 

下面是一些attributes和properties的列表,正常情况下,应该使用其对应的方法(见下面的列表)来取得和设置它们。下面的是首用法,但是.attr()方法可以运行在所有的attributes情况下。

 

注意:一些DOM元素的properties也被列在下面,但是仅运行在新的.prop()方法中


<iframe id="iframe_0.42308900714851916" style="margin: 0px; padding: 0px; border: none; width: 297px; height: 780px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://dl.iteye.com/upload/attachment/482401/bf5b6182-ffc7-3e30-a159-75ac8980fd1f.jpg?_=3799895%22%20style=%22border:none;max-width:1014px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.42308900714851916',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

 

*例如: window.location
**如果需要在(if needed over) .width()

 

.attr()和.prop()都不应该被用来取值/设值。使用.val()方法代替(即使使用.attr("value","somevalue") 可以继续运行,就像1.6之前做的那样)

 

3、首选用法的概述

 

.prop()方法应该被用来处理boolean attributes/properties以及在html(比如:window.location)中不存在的properties。其他所有的attributes(在html中你看到的那些)可以而且应该继续使用.attr()方法来进行操作。

 

 
posted @ 2014-06-20 17:44  KeenLeung 阅读(40663) 评论(5)  编辑  收藏

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326971535&siteId=291194637