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]