topic description
limit the number of check boxes and write them out separately in a single method. When you enter a number in the text box to select an element of the check box, there is no response to the limit.
sources of topics and their own ideas
Source: a topic of JavaScript + my own thoughts.
idea: first judge whether the multi-check box is selected. If it has started to select, call the method to limit the number of multi-check box selected. It is said on the Internet that you want to set the click-to-multi-box trigger method, and then set checked to false, in this method, but the text box control multi-box does not need to click on the multi-box. All cannot trigger the method. After the multi-box checked=false is set with the onclick method, the multi-box element is still selected. I checked a lot of information, but it hasn"t been solved.
related codes
< body >
<form>
:<br>
<input type="checkbox" name="hobby" id="hobby1">
<input type="checkbox" name="hobby" id="hobby2">
<input type="checkbox" name="hobby" id="hobby3">
<input type="checkbox" name="hobby" id="hobby4">
<input type="checkbox" name="hobby" id="hobby5">
<input type="checkbox" name="hobby" id="hobby6"> <br>
<input type="button" value = "" onclick = "checkall();">
<input type="button" value = "" onclick = "clearall();">
1-6:
<input id="wb" name="wb" type="text" >
<input name="ok" type="button" value="" onclick = "checkone();">
</form>
<script type="text/javascript">
function checkall(){
//
var hobby = document.getElementsByTagName("input");
for(var i=0;i<hobby.length;iPP){
if(hobby[i].type=="checkbox"){
hobby[i].checked=true;
}
}
}
function clearall(){
//
var hobby = document.getElementsByName("hobby");
for(var i=0;i<hobby.length;iPP){
if(hobby[i].checked==true){
hobby[i].checked=false;
}
}
}
function checkone(){
//
var num=0;
var j=document.getElementById("wb").value;
var hobby=document.getElementsByName("hobby");
if(j<=0||j>6){
alert("");
}else
{
hobby[j-1].checked=true;
}
for(var i=0;i<hobby.length;iPP){
if(hobby[i].checked==true){
autoCheck("hobby",3);
}
}
}
//
function autoCheck(name,limit){
var hobby=document.getElementsByName(name);
function check()
{
var num=0;
for(var i=0;i<hobby.length;iPP)
{
if(hobby[i].checked){numPP}
if(num>limit)
{
return false;
}
}
return true;
}
function checkedNum(){
for(var i=0;i<hobby.length;iPP){
if(!check()){
alert(""+limit+"!");
hobby[i].checked=false;
}
}
}
}
</script>
</body>
what result do you expect? What is the error message actually seen?
expectation: whether you click the checkbox or enter the numeric control checkbox in the text box, you can no longer select it when the constraint is reached.
error: the text box input numeric control marquee cannot trigger the method of restricting the marquee, which is not consistent with the method of triggering when the marquee is clicked.