JS checkbox control operation code

Source: Internet
Author: User

<Html> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Example 2:
Functions to be implemented:
The js Code of the all-selected checkbox is as follows:
Var status = true;
Function allselect (){
Var tags = document. getElementsByTagName ("input ");
For (I = 0; I <tags. length; I ++)
{
If (tags [I]. type = "checkbox ")
{
Tags [I]. checked = status;
}
}
Status =! Status;
}
When you click show position, apply for the selected position, and add it to favorites, The js Code is as follows:
Function panduan (){
Var gou = 0;
Var tags = document. getElementsByTagName ("input ");
For (I = 0; I <tags. length; I ++)
{
If (tags [I]. type = "checkbox ")
{
If (tags [I]. id! = "Checkbox1") {// here, Checkbox1 is the all-selected checkbox
If (tags [I]. checked = true ){
Gou ++;
}
}
}
}
If (gou = 0 ){
Window. alert ("Please check the selected position! ");
Return false;
}
}
Example 3:
<HTML> <BODY> <input type = "checkbox" name = "huang" value = "ON"> <input type = "checkbox" name = "huang" value = "OFF "> <input type =" button "value =" checkall "onclick =" checkall (); "> <input type =" button "value =" centerall "onclick =" centerall (); "> </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
By the way, let's take a look at the usage of getElementsByTagName.
Javascript provides two methods for getting document reference: getElementById and getElementsByTagName. the former returns an element reference pointing to an element with the specified Id attribute, and the latter returns an array of elements with this tag. The former is widely used and familiar with it, this article briefly explains the latter.
For example, to define a table with multiple td tags, you need to change the background color of the second cell. You can use getElementsByTagName. The Code is as follows:
......
<Table style = "width: 100%;">
<Tr> <td> 1 </td>
<Td> 2 </td>
<Td> 3 </td>
</Tr>
<Tr>
<Td> 4 </td>
<Td> 5 </td>
<Td> 6 </td>
</Tr>
<Tr>
<Td> 7 </td>
<Td> 8 </td>
<Td> 9 </td>
</Tr>
</Table>
......
Var tdObject = document. getElementsByTagName ("td"). item (1 );
TdObject. style. backgroundColor = "blue ";
......

Related Article

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.