Operations for implementing the check box in js ------- Day41

Source: Internet
Author: User

I don't know why the previous article has been in the review stage. Is there any word that is sensitive?

No matter what it is, re-writing this article will deepen your memory.

The first thing to do is to select all and all of the check box checkbox used for table data operations today. First, write the html language

<table>    <thead>    <tr>    <td><input type="checkbox" id="sall" onchange="changeAll()"></td>    </tr>    </thead>    <tbody>    <tr>    <td><input type="checkbox" name="each" value="1"></td>    </tr>    <tr>    <td><input type="checkbox" name="each" value="2"></td>    </tr>    <tr>    <td><input type="checkbox" name="each" value="3"></td>    </tr>    <tr>    <td><input type="checkbox" name="each" value="4"></td>    </tr>    </tbody>    </table>
I want to write a simple and clear way to achieve it after I search for it from the Internet. It is more than twice as good as I write.

Function changeAll () {var sall = document. getElementById ("sall"); // obtain the operation object var seach = document in the title bar. getElementsByName ("each"); // obtain the content bar object for (var I = 0; I <seach. length; I ++) {if (sall. checked) {seach [I]. checked = true;} else {seach [I]. checked = false ;}}}
It is much better to analyze its implementation method than to write it. At that time, I adopted two methods, but it is far less clear than the introduction of this model, it's a long time to use all your IDs for operations.

There are also common check boxes for operations on check boxes. In the above example, we will implement onchange = "change ()"

function change(){var seach=document.getElementsByName("each");for(var i=0;i<seach.length;i++){seach[i].checked=!seach[i].checked;}}
Finally, the most common check box is to get the information of a column to realize the value transfer. Here we get the following value:

var seach=document.getElementsByName("each");for(var i=0;i<seach.length;i++){if(seach[i].checked){alert(seach[i].value);
}}

I don't know what I insist on. If I write it again, the effect is actually not as good as the first time. No matter how much it is, come on, cainiao flying ....



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.