check uncheck All checkboxes with another single checkbox use jquery

后端 未结 10 539
长情又很酷
长情又很酷 2020-12-03 05:14

I use jquery-1.9.1.js In my html page, it works well for the first time.

just like http://jsfiddle.net/pzCcE/1/

Can somebody help me to improve it?

10条回答
  •  攒了一身酷
    2020-12-03 05:44

    Change

    $(this).attr("checked", true);
    

    to

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

    jsFiddle example

    I actually just answered another question that was similar to this. Per the .prop() docs:

    The .prop() method is a convenient way to set the value of properties—especially when setting multiple properties, using values returned by a function, or setting values on multiple elements at once. It should be used when setting selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, or defaultSelected. Since jQuery 1.6, these properties can no longer be set with the .attr() method. They do not have corresponding attributes and are only properties.

    Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value.

提交回复
热议问题