【问题标题】:Limiting user for number of checkboxes to checked限制用户检查复选框的数量
【发布时间】:2016-10-25 15:36:42
【问题描述】:

我正在尝试开发一个页面,其中有许多复选框,但用户只能选中 2 个复选框。我得到了这段代码,但它不起作用,因为我将它命名为数组。请建议在调用 javascript 函数时如何将此名称用于复选框。

function checkboxlimit(checkgroup, limit){
    var checkgroup=checkgroup;
    var limit=limit;
    for (var i=0; i<checkgroup.length; i++){
        checkgroup[i].onclick=function(){
            var checkedcount=0;
            for (var i=0; i<checkgroup.length; i++)
                checkedcount+=(checkgroup[i].checked)? 1 : 0;
            if (checkedcount>limit){
                alert("You can only select a maximum of "+limit+" checkboxes");
                this.checked=false;
            }
        }
    }
}
checkboxlimit(document.forms.world.seatdata, 2);
<form id="world" name="world">
<table>
<tr>
<td>
  <input type='checkbox' name='seatdata[]' value='0|12' id="A11" />
  <label for="A11">A11</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|11' id="A10"  />
  <label for="A10">A10</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|10' id="A9"  />
  <label for="A9">A9</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|9' id="A8"  />
  <label for="A8">A8</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|6' id="A7"  />
  <label for="A7">A7</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|5' id="A6"  />
  <label for="A6">A6</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|4' id="A5"  />
  <label for="A5">A5</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|3' id="A4"  />
  <label for="A4">A4</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|2' id="A3" />
  <label for="A3">A3</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|1' id="A2" />
  <label for="A2">A2</label>
</td>
<td>
  <input type='checkbox' name='seatdata[]' value='0|0' id="A1" unchecked />
  <label for="A1">A1</label>
</td>
</tr>
</table>
</form>

【问题讨论】:

  • @HudsonPH 可能不是重复的。那是一个jQuery答案。这个是纯js问题
  • @HudsonPH 但它是 jquery 我想用 javascript 来做

标签: javascript arrays forms function checkbox


【解决方案1】:

只要改变这一行

checkboxlimit(document.forms.world.seatdata, 2);checkboxlimit(document.forms.world["seatdata[]"], 2);

或使用document.getElementsByName

checkboxlimit(document.getElementsByName("seatdata[]"), 2);

【讨论】:

    【解决方案2】:

    对于纯 javascript -

    checkboxlimit(document.getElementsByName("seatdata[]"), 2);
    

    对于 jQuery 用户 -

    HTML 代码-

    <input type="checkbox" class="abc" />hello1
    <input type="checkbox" class="abc" />hello2
    <input type="checkbox" class="abc" />hello3
    <input type="checkbox" class="abc" />hello4
    <input type="checkbox" class="abc" />hello5
    <input type="checkbox" class="abc" />hello6
    <input type="checkbox" class="abc" />hello7
    <input type="checkbox" class="abc" />hello8
    

    Jquery 代码-

    var limit = 2;
    var count = 0;
    $('.abc').change(function(){
        count++;
        if(count > limit){
          alert("You cant select more than "+limit+" checkboxes");
          this.checked=false;
        }
    });
    

    【讨论】:

      【解决方案3】:

      你不能用吗

      checkboxlimit(document.getElementsByName('seatdata[]'), 2);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 2023-04-01
        相关资源
        最近更新 更多