Yesterday morning, I wrote about how to use jquery to make all the choices.
According to everyone's opinions on the procedures in some of the bad writing, but also the level of my limited, there are a variety of places to consider.
At the end of the article I raised a question, to write a general method to call, so I have the present article, the evening home, I wrote the effect
The following examples can be used to discuss learning, if you feel good can also be applied directly to the project.
1: Why write this method
Online to achieve a full selection of the whole election there are many, but it seems to ignore a problem, the checkbox can be selected to control the following child checkbox, but the following child checkbox should also be able to control the full selection, so that I have this method.
2: The development process: to achieve universal will solve 2 problems, how to group, how to determine which checkbox in the group for the full election, I think, think with the name group can,
Use class to determine whether a checkbox group that requires a full selection. Find all classes First and then find the Name property, and you can find the other child checkbox based on the Name property, as long as you find the element, you can do it.
3: File, the following is my implementation of JS, save for Xs_checkbox_all.js
Copy Code code as follows:
$ (document). Ready (function () {
var xschk = "Xschk";//Style defined
var xschkall = "input[type= ' checkbox '][class= '" + xschk + "'][name]";/all checkbox that defines this style
$ (Xschkall). each (function () {
var name = $ (this). attr ("name");
Name = "input[type= ' checkbox '][class!= '" + xschk + "'][name= '" + name + "']";//the Child checkbox under this full selection box
$ (this). Click (function () {
$ (name). attr ("Checked", $ (this) [0].checked);
})
var Xschk = $ (this);
$ (name). Click (function () {
var Iall = $ (name). length; The number of all the checkbox under this child item
var ichk = $ (name + ": Checked"). Length; The number of all checked checkboxes under this sub item
var isallchecked = true; Whether it is a full selection
if (Iall!= ichk) {
isallchecked = false;
}
$ (XSCHK). attr ("Checked", isallchecked);
});
});
});
Page Use
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<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> Full Selection 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> Full Selection two</legend>
<input type= "checkbox" class= "Xschk" name= "Chk1"/>
All options 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>
Can achieve results, if there is any optimization of the place, please put forward, I will do better, thank you, the above is the entire content of this article, I hope you can enjoy.