全选反选JavaScript实现

Myth丶恋晨 2022-05-27 12:36 357阅读 0赞

html代码

  1. <label for="all">全选</label><input type="checkbox" id="all">
  2. <div id="box">
  3. <input type="checkbox">
  4. <input type="checkbox">
  5. <input type="checkbox">
  6. <input type="checkbox">
  7. <input type="checkbox">
  8. <input type="checkbox">
  9. </div>

JS代码

  1. var all = document.getElementById("all");
  2. var aInput = document.querySelectorAll("#box>input");
  3. all.onclick = function () {
  4. if (this.checked) {
  5. for (var i = 0; i < aInput.length; i++) {
  6. aInput[i].checked = true;
  7. }
  8. } else {
  9. for (var i = 0; i < aInput.length; i++) {
  10. aInput[i].checked = false;
  11. }
  12. }
  13. };
  14. for (var i = 0; i < aInput.length; i++) {
  15. aInput[i].onclick = function () {
  16. var bStop = true;
  17. for (var K = 0; K < aInput.length; K++) {
  18. if (!aInput[K].checked) {
  19. bStop = false;
  20. break;
  21. }
  22. }
  23. if (bStop) {
  24. all.checked = true;
  25. } else {
  26. all.checked = false;
  27. }
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读