SoFunction
Updated on 2025-02-28

JS code that limits the maximum number of optionals for check boxes

In some web applications, for example, in the voting system, when we are voting multiple votes, we require users to only choose a few items to vote at most, which means that the number of check boxes is selected at most.
So how to set up the most of these, we need to do some simple scripting. The following codes are all excerpted from the Internet, and the author is unknown.
For example, the following code solves the related problems:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

The most important thing to pay attention to when changing the code is
//The name prefix of the checkbox element, this example is sample1, sample2, sample3...
var sCtrlPrefix = "sample";
and
//The number of checkbox elements, there are 10 in this example;
var iMaxCheckbox = 10;
The prefix of the name and number of elements of the checkbox element are invalid if the slight error is defined in the page.
Therefore, the following script functions are general:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]


[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]