How to Use checkbox in javascript simple example _ javascript skills

Source: Internet
Author: User
This article demonstrates how to use the checkbox in javascript through simple examples. If you are interested, you can refer to this example to demonstrate a small example: in the shopping cart, we can select the items we have selected, then the corresponding price is displayed.
1. First, the corresponding interface is displayed:

Related code:

Product list:
Laptop3000 Desktop2900 vro90
Regular necessities500 Washing Machine5600
Select All

Note: In the checkbox, if it belongs to the same group, the name = "mm" attribute must be the same in the attribute of the check box; the option can be easily traversed at that time; in radio, name = "mm" must also be set to make it easier for the same group to differentiate each other.
2. Select All button settings


Related code:

function allCheck(node1){     var node=document.getElementsByName("mm");     for (var x = 0; x < node.length; x++) {      node[x].checked=node1.checked;     }    } 

Additional:When all calls are selectedAfter the function in it, it will traverse all objects with the same name and set the status of all check boxes to checked = true.
3. All statuses are selected and automatically selected.
Code implementation:

Function chose (node) {var flag = true; // used to traverse whether all variables are set var allM = document. getElementsByName ("all") [0]; var node = document. getElementsByName ("mm"); for (var x = 0; x <node. length; x ++) {if (node [x]. checked = false) {// exit traversal as long as one is not selected, flag is set to false flag = false; break ;}} if (flag) {allM. checked = true;} else {allM. checked = false ;}}

4. Click the View button to call the function.

Function sumall () {var sum = 0; var names = document. getElementsByName ("mm"); for (var x = 0; x
 
  

Summary:
1). This mainly refers to the application of the check box. After the check box is selected, how can we get the corresponding content?

2) if the name is in the same group, the name = "mm" attribute must be consistent in the checkbox;

Var names = document. getElementsByName ("mm"); you can use this to determine whether to select and call value to perform the checked setting or obtain the status of the check box or single-choice button. And add them one by one.

3). Select all settings. Similarly, get the object array through document. getElementsByName, and then pay true one by one.

When one is not selected, we set the all-selected button to checked = false. The mark is used to distinguish between them. If the mark does not change, it indicates that no one is selected, ).

The usage of checkbox in javascript is described in detail.

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.