如何获取JavaScript中未选中的复选框?

huangapple go评论61阅读模式
英文:

How to get the unchecked checkboxes in a js?

问题

var checkBoxesEnabled = document.querySelectorAll('.form-check-input');
var limit = 2;
for (var i = 0; i < checkBoxesEnabled.length; i++) {
   checkBoxesEnabled[i].addEventListener('click', function () {
       var uncheckedcount = 0;
       for (var i = 0; i < checkBoxesEnabled.length; i++) {
           if (checkBoxesEnabled[i].checked === false) {
               uncheckedcount++;
           }
       }
       if (uncheckedcount > limit) {
           alert('You cannot disable more than 2');
       }
   });
}
<div id="excludedNumbers">
  <div class="form-group">
    <div class=" form-check">
        <input class="form-check-input" checked type="checkbox" value="0" id="add0">
        <label class="form-check-label" for="add0">0</label>
     </div>
     <div class="form-check">
         <input class="form-check-input" checked type="checkbox" value="1" id="add1">
         <label class="form-check-label" for="add1">1</label>
     </div>
     <div class="form-check">
        <input class="form-check-input" checked type="checkbox" value="2" id="add2">
        <label class="form-check-label" for="add2">2</label>
     </div>
     <div class="form-check">
         <input class="form-check-input" checked type="checkbox" value="3" id="add3">
         <label class="form-check-label" for="add3">3</label>
     </div>
     <div class="form-check">
         <input class="form-check-input" checked type="checkbox" value="4" id="add4">
         <label class="form-check-label" for="add4">4</label>
     </div>
      <div class="form-check">
         <input class="form-check-input" checked type="checkbox" value="5" id="add5">
         <label class="form-check-label" for="add5">5</label>
     </div>
  </div>
</div>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var checkBoxesEnabled =document.querySelectorAll(&#39;.form-check-input&#39;);
var limit = 2;
for (var i = 0; i &lt; checkBoxesEnabled.length; i++) {
checkBoxesEnabled[i].addEventListener(&#39;click&#39;, function () {
var uncheckedcount = 0;
for (var i = 0; i &lt; checkBoxesEnabled.length; i++) {
if(checkBoxesEnabled[i].checked === false &gt; limit){
alert(&#39;You cannot disable more than 2&#39;);
}
}
});
}

<!-- language: lang-html -->

&lt;div id=&quot;excludedNumbers&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot; form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;0&quot; id=&quot;add0&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add0&quot;&gt;0&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;1&quot; id=&quot;add1&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add1&quot;&gt;1&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;2&quot; id=&quot;add2&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add2&quot;&gt;2&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;3&quot; id=&quot;add3&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add3&quot;&gt;3&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;4&quot; id=&quot;add4&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add4&quot;&gt;4&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;5&quot; id=&quot;add5&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add5&quot;&gt;5&lt;/label&gt;
&lt;/div&gt;

<!-- end snippet -->

I have a 5 checkboxes, Where all of them were checked.

The maximum 2 should be unchecked, with all the 3 checked.

It didnt work, not sure what was wrong

How could I get the alert, if more than 2 checkboxes were unchecked?

答案1

得分: 1

你需要在每次找到选中的复选框时增加计数器的值,然后与限制变量的值进行比较。如果超过限制,就会弹出警报。

var checkBoxesEnabled = document.querySelectorAll('.form-check-input');
var limit = 2;
for (var i = 0; i < checkBoxesEnabled.length; i++) {
    checkBoxesEnabled[i].addEventListener('click', function () {
        var uncheckedCount = 0;
        for (var i = 0; i < checkBoxesEnabled.length; i++) {
            if (checkBoxesEnabled[i].checked === false) uncheckedCount++;
            if (limit < uncheckedCount) {
                alert('You cannot disable more than 2');
                break;
            }
        }
    });
}
英文:

You need to increase the counter each time you find a checked checkbox and then compare it with the value of the limit variable. Raise an alert if the limit is crossed.

var checkBoxesEnabled =document.querySelectorAll(&#39;.form-check-input&#39;);
var limit = 2;
for (var i = 0; i &lt; checkBoxesEnabled.length; i++) {
checkBoxesEnabled[i].addEventListener(&#39;click&#39;, function () {
var uncheckedcount = 0;
for (var i = 0; i &lt; checkBoxesEnabled.length; i++) {
if(checkBoxesEnabled[i].checked === false) uncheckedcount++;
if (limit &lt; uncheckedcount) {
alert(&#39;You cannot disable more than 2&#39;);
break;
}
}
});
}

答案2

得分: 1

1- 在你的for循环中使用let

2- 不需要在另一个循环中使用循环。

3- 只需使用一个计数器来累加复选框未选中的次数,然后与你的限制进行比较。

英文:

1- use let in your for loop

2- no need for a loop inside another loop

3- just use a counter to sum how many times a checkbox was unchecked and then compare to your limit

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const checkBoxesEnabled = document.querySelectorAll(&#39;.form-check-input&#39;);
const limit = 2;
let uncheckedcount = 0;
for (let i = 0; i &lt; checkBoxesEnabled.length; i++) {
checkBoxesEnabled[i].addEventListener(&#39;click&#39;, function() {
if (checkBoxesEnabled[i].checked === false) {
uncheckedcount++;
} else {
uncheckedcount--;
}
if (uncheckedcount &gt; limit) {
alert(&#39;You cannot disable more than 2&#39;);
}
});
}

<!-- language: lang-html -->

&lt;div id=&quot;excludedNumbers&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot; form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;0&quot; id=&quot;add0&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add0&quot;&gt;0&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;1&quot; id=&quot;add1&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add1&quot;&gt;1&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;2&quot; id=&quot;add2&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add2&quot;&gt;2&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;3&quot; id=&quot;add3&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add3&quot;&gt;3&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;4&quot; id=&quot;add4&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add4&quot;&gt;4&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; checked type=&quot;checkbox&quot; value=&quot;5&quot; id=&quot;add5&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;add5&quot;&gt;5&lt;/label&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 21:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定