Jquery implements the full checkbox selection method, jquerycheckbox
Yesterday morning I wrote how to use Jquery to achieve full selection.
According to your comments, I have modified some of the difficult-to-write aspects of the program, and I am still at a limited level. There are various issues that cannot be considered.
At the end of the article, I raised a question. I had to write a general method to call it, so now I had this article. When I got home at night, I wrote the results.
The following example can be used for discussion and study. If you think it is good, you can directly apply it to the project.
1: Why write this method?
There are a lot of questions about selecting all one sentence on the Internet, but it seems that a problem is ignored. The fully selected checkbox can control the following subcheckbox, however, the following sub-checkbox should also be able to control all the above options, so that I can use this method.
2: Development Process: to achieve general purpose, we need to solve two problems: how to group, how to determine which checkbox in the group is all selected, I thought, I think it is okay to use name grouping,
Use class to determine whether the checkbox group needs to be fully selected. First, find all the classes that need to be fully selected, and then find the name attribute. Based on the name attribute, you can find other subcheckboxes. You only need to find the elements and perform operations.
3: file. below is my implemented js, saved as xs_checkbox_all.js
Copy codeThe Code is as follows:
$ (Document). ready (function (){
Var xsChk = "xsChk"; // defines the style.
Var xsChkAll = "input [type = 'checkbox'] [class = '" + xsChk + "'] [name]"; // all the checkboxes that define this style
$ (XsChkAll). each (function (){
Var name = $ (this). attr ("name ");
Name = "input [type = 'checkbox'] [class! = '"+ XsChk +"'] [name = '"+ name +"'] "; // subcheckbox under the all-selected box
$ (This). click (function (){
$ (Name). attr ("checked", $ (this) [0]. checked );
})
Var xschk = $ (this );
$ (Name). click (function (){
Var IAll = $ (name). length; // The number of all checkpoints in this subitem
Var IChk = $ (name + ": checked"). length; // The number of checkboxes selected under this subitem
Var isAllChecked = true; // whether it is all selected
If (IAll! = IChk ){
IsAllChecked = false;
}
$ (Xschk). attr ("checked", isAllChecked );
});
});
});
Page usage
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
<Script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js" type = "text/javascript">
</Script>
<Script src = "xs_checkbox_all.js" type = "text/javascript"> </script>
<Fieldset>
<Legend> select all one </legend>
<Input type = "checkbox" class = "xsChk" name = "chk"/>
Select All
<Div>
<Input type = "checkbox" name = "chk"/>
1 <br/>
<Input type = "checkbox" name = "chk"/>
2 <br/>
<Input type = "checkbox" name = "chk"/>
3 <br/>
<Input type = "checkbox" name = "chk"/>
4 <br/>
</Div>
</Fieldset>
<Fieldset>
<Legend> select two </legend>
<Input type = "checkbox" class = "xsChk" name = "chk1"/>
Select 2
<Div>
<Input type = "checkbox" name = "chk1"/>
11 <br/>
<Input type = "checkbox" name = "chk1"/>
22 <br/>
<Input type = "checkbox" name = "chk1"/>
33 <br/>
<Input type = "checkbox" name = "chk1"/>
44 <br/>
</Div>
</Fieldset>
</Body>
</Html>
If you have any optimizations, please advise. I will do better. Thank you. The above is all the content in this article. I hope you will like it.