Limit the maximum number of optional js code_form effects of the check box

Source: Internet
Author: User
A good limitation is that the maximum number of check boxes selected at the same time is in some web applications. For example, in the voting system, when we perform multiple votes, we require that you select only a few items for voting, which means that you can select a maximum of several check boxes.
So how to set the most, we need to do some simple script processing. The following code is excerpted from the Internet, which is unknown to the author.
For example, the following code solves the problem:

<Script language = "JavaScript"> <! -- // The name prefix of the checkbox element. In this example, sample1, sample2, sample3... var sCtrlPrefix = "sample"; // Number of checkbox elements. In this example, there are 10; var iMaxCheckbox = 10; // you can specify the maximum number of elements that can be selected. var iMaxSelected = 3; function doCheck (ctrl) {var iNumChecked = 0; var thisCtrl; var I; // initialize I = 1; // loop until the most checkbox is selected; while (I <= iMaxCheckbox) & (iNumChecked <= iMaxSelected) {thisCtrl = eval ("ctrl. form. "+ sCtrlPrefix + I); if (thisCtrl! = Ctrl) & (thisCtrl. checked) {iNumChecked ++;} I ++;} // check whether the maximum number of selected items has been reached; if (iNumChecked = iMaxSelected) {// If yes, uncheck the selected element; ctrl. checked = false; ALERTXT = "optional" + iMaxSelected + "" alert (ALERTXT) ;}// --> SCRIPT
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Note that
// The name prefix of the checkbox element. In this example, sample1, sample2, sample3...
Var sCtrlPrefix = "sample ";
And
// Number of checkbox elements. In this example, there are 10;
Var iMaxCheckbox = 10;
The name prefix and number of elements of the checkbox element are invalid when there is a slight error script defined on the page.
Therefore, the following script functions are common:

<Script language = "JavaScript"> var c = 0, limit = 3; function doCheck (obj) {obj. checked? C ++: c --; if (c> limit) obj. checked = false, c --;} SCRIPT
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


<Script language = "JavaScript"> var c = 0, limit = 3; ALERTXT = "up to" + limit + "function doCheck (obj) {obj. checked? C ++: c --; if (c> limit) obj. checked = false, c --, alert (ALERTXT);} SCRIPT
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.