When you enter a fixed number in the text box, why does it have no effect by selecting an element of the multi-check box and limiting the number of multi-check boxes?

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.

Mar.26,2021

replace the code in your script with the following code to implement the requirement. In order to make it easier to write directly, I use the es6 syntax. Make sure your browser supports

.
const limit = 3;

const hobby = [].map.call(document.querySelectorAll("input[name='hobby']"), el => el);

const checkedLength = () => hobby.filter(el => el.checked).length;

hobby.forEach(el => {
  el.addEventListener('change', e => {
    if (checkedLength() > limit) {
      e.target.checked = false;
    }
  });
});

function checkall() {
  hobby.forEach(el => el.checked = true)
}

function clearall() {
  hobby.forEach(el => el.checked = false)
}

function checkone() {
  const index = document.getElementById("wb").value;
  if (index > 0 && index < 6) {
    const el = hobby[index - 1];
    if (!el.checked) {
      if (checkedLength() < limit) {
        el.checked = true;
      } else {
        alert("" + limit + "!");
      }
    }
  } else {
    alert("");
  }
}

changed a little bit on your original basis

<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 j = document.getElementById("wb").value; var hobby = document.getElementsByName('hobby'); if (j <= 0 || j > 6) { alert(""); } else if (autoCheck("hobby", 3, 'checkone')) { hobby[j - 1].checked = true; } } // function autoCheck(name, limit, type) { var hobby = document.getElementsByName(name); if (type == 'checkone') { var num = 1; } else { var num = 0; } for (var i = 0; i < hobby.length; iPP) { if (hobby[i].checked) { numPP } } console.log(num) if (num <= limit) { return true; } else { alert('') return false; } } // function sigleCheck() { var hobby = document.getElementsByName('hobby'); if (autoCheck("hobby", 3)) { for (var i = 0; i < hobby.length; iPP) { hobby[i].onclick = function (e) { if (autoCheck("hobby", 3, 'sigleCheck')) { e.target.checked = true; } else { e.target.checked = false; } } } } } sigleCheck() </script> </body>
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb2690-48195.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb2690-48195.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?