简体   繁体   English

复选框的可变限制数

[英]Variable limit number of checkboxes

I would like to limit number of checkboxes not with a fix number (ie2), but I want pre-select this limit. 我想限制复选框的数量,而不用固定数字(ie2),但我想预选此限制。

I'm able to limit in a fix way (working example) 我能够以一种固定的方式进行限制(工作示例)

 <p>Select your favorite countries below:</p>

<form id="world" name="world">
Select the limit<select id="limit" onchange="this.form.submit()"><option value="1">1</option><option value="2">2</option></select><br/>
 <input type="checkbox" name="countries" /> USA<br />
 <input type="checkbox" name="countries" /> Canada<br />
 <input type="checkbox" name="countries" /> Japan<br />
 <input type="checkbox" name="countries" /> China<br />
<input type="checkbox" name="countries" /> France<br />
</form>


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.countries, 2)

but I'm not able to to change the limit in this variable way (not working) 但我无法以这种可变的方式更改限制(无效)

<p>Select your favorite countries below:</p>

<form id="world" name="world">
Select the limit<select id="limit" onchange="this.form.submit()"><option value="1">1</option><option value="2">2</option></select><br/>
<input type="checkbox" name="countries" /> USA<br />
<input type="checkbox" name="countries" /> Canada<br />
<input type="checkbox" name="countries" /> Japan<br />
<input type="checkbox" name="countries" /> China<br />
<input type="checkbox" name="countries" /> France<br />
</form>



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
        }
    }
}
}

var e = document.getElementById("limit");
var limit = e.options[e.selectedIndex].value;
checkboxlimit(document.forms.world.countries, limit)

Here is a possible solution: https://jsfiddle.net/g6cyegnt/1/ 这是一个可能的解决方案: https : //jsfiddle.net/g6cyegnt/1/

All you have to do is, do not submit the form on the change event of limit control. 您所要做的就是,不要在限制控制的更改事件上提交表单。 Handle the change event separately as shown in the code / fiddler. 如代码/提琴手所示,分别处理更改事件。

<p>Select your favorite countries below:</p>

<form id="world" name="world">
    Select the limit<select id="limit"><option value="1">1</option><option value="2">2</option></select><br/>
<input type="checkbox" name="countries" /> USA<br />
<input type="checkbox" name="countries" /> Canada<br />
<input type="checkbox" name="countries" /> Japan<br />
<input type="checkbox" name="countries" /> China<br />
<input type="checkbox" name="countries" /> France<br />
</form>

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
            }
        }
    }
}

$('input[name="countries"]').on('change', function(){
var limit = $('#limit').find(':selected').val();
checkboxlimit(document.forms.world.countries, limit)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM