The effect I want to do is to automatically select all the checkboxes when we open the page. The principle is to first obtain all elements whose type is \ 'input, the selected option is used to add a click event. If the full option is selected, the \ 'checked \ 'attribute of each element is changed to true through a loop. Otherwise, the attribute is changed to false.
Document. getElementById ('') is the id feature of an element to obtain a specified element. An object is returned. Document. getElementById ('') obtains the control object based on the control TAG (type attribute) and returns an array of objects;
To achieve a full-selection effect, you must define a set of options first.
The Code is as follows: |
Copy code |
<Body> <P id = "btn"> <input id = "hk" type = "checkbox"/> select all </p> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> <Input type = "checkbox"/> <br/> </Body> |
Next, use Javascript to achieve the effect. The effect in the tutorial is that you can only select all selected items when you click all. If you click all selected items, you cannot select all items. Here, I have added this function through my understanding.
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Window. onload = function (){
Var oInput = document. getElementsByTagName ('input ') Var ohk = document. getElementById ('hk ') Ohk. onclick = function (){ Var I = 0; If (ohk. checked = true ){ For (I = 0; I <oInput. length; I ++) { OInput [I]. checked = true; } } Else { For (I = 0; I <oInput. length; I ++) { OInput [I]. checked = false;
}
} }; }; </Script> |
I personally feel that this method is still lacking. He will select all the types on the page as input. Now we have written a click event to select all.
The Code is as follows: |
Copy code |
<Meta charset = "UTF-8"> <Script language = "javascript"> Function selectAll () { Obj = document. getElementsByName ('range '); For (var I = 0; I <obj. length; I ++) { // Document. form1.range [I]. checked = true; Obj. item (I). checked = true; } } Function unSelect () { Obj = document. getElementsByName ('range '); For (var I = 0; I <obj. length; I ++) { If (obj. item (I). checked) { Obj. item (I). checked = false; } Else { Obj. item (I). checked = true; } } } </Script> <Form name = "form1"> <Input type = "checkbox" value = "1" name = "range"> 1 <br> <! -- <Input type = "checkbox" value = "3" name = "range"> 3 <br> <Input type = "checkbox" value = "4" name = "range"> 4 <br> <Input type = "checkbox" value = "5" name = "range"> 5 <br> --> <Input type = "button" value = "select all" onclick = "selectAll ();"> <Input type = "button" value = "invert" onclick = "unSelect ();"> </Form>
|
In this way, if the deletion function is also implemented in combination with the php program, we only need to provide the obtained string in post or get mode. The following describes how to provide the get method.
The Code is as follows: |
Copy code |
// Batch Delete // Na is name Function checkSubmit (na, url) { Var str = ''; For (var I = 0; I <document. getElementsByName (na). length; I ++) { If (document. getElementsByName (na) [I]. checked ){ If (str = '') str + = document. getElementsByName (na) [I]. value; Else str + = ',' + document. getElementsByName (na) [I]. value; } } If (str = '') { Alert ('You didn't select any content! '); Return false; } Else { Location = url + "? Id = "+ str +" & action = delall "; } } |
In the php program
The Code is as follows: |
Copy code |
$ A = $ _ GET ['id']; |
Then, you can use where id in ($ a) to delete it. This is also a simple php tutorial. If you need it, you can learn and communicate with others.