Javascript 将用户的复选框数限制为选中

Javascript 将用户的复选框数限制为选中,javascript,arrays,forms,function,checkbox,Javascript,Arrays,Forms,Function,Checkbox,我试图开发一个页面,其中有许多复选框,但用户仅限于选中2个复选框。我得到了这个代码,但它不工作,因为我给它命名为数组。请建议在调用javascript函数时如何使用此名称作为复选框 功能检查框限制(检查组,限制){ var checkgroup=checkgroup; var限额=限额; 对于(var i=0;i只需更改这一行即可 checkboxlimit(document.forms.world.seatdata,2);tocheckboxlimit(document.forms.world

我试图开发一个页面,其中有许多复选框,但用户仅限于选中2个复选框。我得到了这个代码,但它不工作,因为我给它命名为数组。请建议在调用javascript函数时如何使用此名称作为复选框

功能检查框限制(检查组,限制){
var checkgroup=checkgroup;
var限额=限额;
对于(var i=0;i只需更改这一行即可

checkboxlimit(document.forms.world.seatdata,2);
to
checkboxlimit(document.forms.world[“seatdata[]”,2);

或者使用
document.getElementsByName

checkboxlimit(document.getElementsByName(“seatdata[]),2);
只需更改此行即可

checkboxlimit(document.forms.world.seatdata,2);
to
checkboxlimit(document.forms.world[“seatdata[]”,2);

或者使用
document.getElementsByName

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

您不能使用

checkboxlimit(document.getElementsByName('seatdata[]),2);
您不能使用

checkboxlimit(document.getElementsByName('seatdata[]),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
对于纯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

@HudsonPH的可能副本可能不是副本。这是jquery的答案。这是一个纯jsquestion@HudsonPH但是它是jquery我想用JavaScription做的@HudsonPH的可能副本可能不是副本。这是jquery的答案。这是一个纯jsquestion@HudsonPH但是它是jquery,我想用javascript实现它