Limit check box The implementation code for the most selected items _javascript tips

Source: Internet
Author: User

In a recruiting website, you can pinpoint the position of a user by limiting the number of job tags you choose. For example, provide the user with an alternative position label as a check box, limit the user to a maximum of 3, and prevent the user from continuing the selection when more than three are available.

check box in the survey, the recruitment site is very broad, today to introduce the Limit check box to select a number of methods:

Ideas:

OnClick Event listening for check boxes

Checkbox.onclick = function () {
 //code block
}

Checked property for the Listener check box:

if (chckbox.checked) {
 //code block
}

HTML code:

< input type= "checkbox" Name= "Sport"/> Basketball <br/>
< input type= "checkbox" Name= "Sport"/> Football <br/& gt;
< input type= "checkbox" Name= "Sport"/> Volleyball <br/>
< input type= "checkbox" Name= "Sport"/> Badminton <br />
< input type= "checkbox" Name= "Sport"/> Table tennis <br/>
< p> select up to three </p>

JavaScript code:

var sportselect = document.getelementsbyname (' Sport '),
              maxnums   = 3;
           for (var i in Sportselect) {
              sportselect[i]. onclick = function () {
                 var _sportselect = Document.getelementsbyname ( ' Sport '),
                   cnums      = 0;
                 for (var i in _sportselect) {
                   if (i = = ' length ') break;
                   if (_sportselect[i].checked) {
                      cnums + +;
                   }
                }
                 if (Cnums > Maxnums) {
                   this.checked = false;
                    Alert (' Up to three options only ');}}
           

The above limit check box to select the most of the implementation code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.