Select All For jquery, cancel all for jquery, and cancel all for jquery
Recently, the project used the "select all" and "unselect all" operation.
I used to write my own JS files. now that you know how to write. so how can we use JQ to write more concisely. in this way, you can learn new things. if Baidu finds a good stuff. thanks to OSCIuhoay.
The Code is as follows:
[Javascript]View plaincopy
- <Script type = "text/javascript" src = "/Webpage/bzz_index/password/js/jquery-1.7.1.min.js"> </script>
- <Script language = "JavaScript">
- $ (Function (){
- $ ("# CkAll"). click (function (){
- $ ("Input [name = 'sub']"). prop ("checked", this. checked );
- });
- $ ("Input [name = 'sub']"). click (function (){
- Var $ subs = $ ("input [name = 'sub']");
- $ ("# CkAll"). prop ("checked", $ subs. length = $ subs. filter (": checked"). length? True: false );
- });
- });
- </Script>
[Html]View plaincopy
- <Input type = "checkbox" id = "ckAll"/> check all <br/>
- <Input type = "checkbox" name = "sub"/> 1 <br/>
- <Input type = "checkbox" name = "sub"/> 2 <br/>
- <Input type = "checkbox" name = "sub"/> 3 <br/>
- <Input type = "checkbox" name = "sub"/> 4 <br/>
It must be said that JQ1.6 + is supported for prop. You can refer to the following section about prop.
I found a problem using JQuery today. attr ("checked") can obtain the checked attribute of the checkbox when it is selected. The value is "checked", but the undefined value is undefined.
To solve this problem, I have referred to this post:
Http://bugs.jquery.com/ticket/9812
Why is jquery 1.6 + added. the prop () method, because in some browsers, for example, you only need to write disabled and checked, and some need to write disabled = "disabled", checked = "checked ". Therefore, starting from 1.6, jq provides a new method "prop" to obtain these attributes. Previously, "checked" and "" were returned when the checked attribute was obtained using attr. Now, if the property is obtained using the prop method, both true and false are returned. So when to use attr and prop ?? 1. Add an attribute name. This attribute will take effect. If prop.2. is true or false, use prop.3. if other attributes are used, pay attention to this when using jquery upgrade in the attr project! We recommend that you use attr () and prop () as follows:
Note the following three points (From dark threads):
JQuery Team provides a reference table for attr ()/prop () For DOM element attributes:
Attribute/Property |
.attr() |
.prop() |
Accesskey |
Bytes |
|
Align |
Bytes |
|
Async |
Bytes |
Bytes |
Autofocus |
Bytes |
Bytes |
Checked |
Bytes |
Bytes |
Class |
Bytes |
|
Contenteditable |
Bytes |
|
DefaultValue |
|
Bytes |
Draggable |
Bytes |
|
Href |
Bytes |
|
Id |
Bytes |
|
Label |
Bytes |
|
Location * |
Bytes |
Bytes |
Multiple |
Bytes |
Bytes |
NodeName |
|
Bytes |
NodeType |
|
Bytes |
ReadOnly |
Bytes |
Bytes |
Rel |
Bytes |
|
Selected |
Bytes |
Bytes |
SelectedIndex |
|
Bytes |
Src |
Bytes |
|
Style |
Bytes |
|
Tabindex |
Bytes |
|
TagName |
|
Bytes |
Title |
Bytes |
|
Type |
Bytes |
|
Width ** |
Bytes |