JS implementation checkbox check box full selection, optional or all-_javascript skills

Source: Internet
Author: User

The CheckBox control indicates whether a particular state (that is, an option) is selected (on, the value is 1) or cleared (off, with a value of 0). Use this control in your application to provide users with a choice of "true/false" or "yes/no". Because the checkbox works independently of each other, the user can select as many checkbox as possible to combine options.

CheckBox check box JS implementation of the full selection, not selected, all the optional function, very simple, the specific content as follows

Ideas:

    • 1. Get the Element
    • 2. Add Click events to select all optional options
    • 3, with For loop checkbox
    • 4, the checkbox checked set to True to achieve the full selection
    • 5, the checkbox checked set to False to achieve the optional
    • 6, if the judge, if checked is true selected state, the checked set to False state, if checked false state, the checked set to true selected state.

HTML code:

 <input type=" button "value=" Select All "id=" Sele "/> <input type=" button "value=" Do not select "Id=" se Tinterval "/> <input type=" button "value=" anti-select "id=" clear "/> <div" id= "Checkboxs" > <input type= "che
    Ckbox "/><br/> <input type=" checkbox "/><br/>" checkbox "<input type= <input type= "checkbox"/><br/> <input type= checkbox/><br/> <input type= "checkbox"/&G  t;<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/> <input type= "checkbox"/><br/> <input "ch Eckbox "/><br/> <input type=" checkbox "/><br/>" <input type= "checkbox"/><br ;/div> 

JS Code:

<script>
window.onload=function () {

var Sele=document.getelementbyid (' Sele ');//Get Select all
var unsele= document.getElementById (' setinterval '); get select Var
clear=document.getelementbyid (' clear ');//Get Inverse
var Checkbox=document.getelementbyid (' Checkboxs ');//Get Div
var checked=checkbox.getelementsbytagname (' input '); Gets the input
//Select All
sele.onclick=function () {for
(i=0;i<checked.length;i++) {
checked[i] under the div. Checked=true
}

//Do not select
unsele.onclick=function () {for
(i=0;i<checked.length;i++) {
checked[i].checked=false
}
}
Reverse-Select
clear.onclick=function () {for
(i=0;i<checked.length;i++) {
if (checked[i].checked==true) {
Checked[i].checked=false
}
else{
checked[i].checked=true
}}}
</script>

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.