Javascript 在多类别jQuery中选择多个复选框
我试图用jQuery配置多个复选框 我想用户可以选择8个复选框内最多2个不同的类别最多 目前用户可以选择8个复选框,但我不知道如何限制为2类Javascript 在多类别jQuery中选择多个复选框,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我试图用jQuery配置多个复选框 我想用户可以选择8个复选框内最多2个不同的类别最多 目前用户可以选择8个复选框,但我不知道如何限制为2类 $(document).ready(function () { $("input[name='tech']").change(function () { var maxAllowed = 8; var cnt = $("input[name='tech']:checked").length; if (
$(document).ready(function () {
$("input[name='tech']").change(function () {
var maxAllowed = 8;
var cnt = $("input[name='tech']:checked").length;
if (cnt > maxAllowed) {
$(this).prop("checked", "");
alert('You can select maximum ' + maxAllowed + ' technologies!!');
}
});
});
完整代码示例:
有什么想法吗?有很多可能的解决方案。例如,使用类。像这样
有许多可能的解决办法。例如,使用类。像这样
一个简单的方法如下:
<h3>Select multi checkbox inside 2 categories max:</h3>
<div class="col-xs-4">
<h5>Category 1</h5>
<input type="checkbox" name="tech1" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 2</h5>
<input type="checkbox" name="tech2" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 3</h5>
<input type="checkbox" name="tech3" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Mootools" /> checkbox
<br/>
</div>
$(document).ready(function () {
var maxAllowedCheckboxes = 8;
$("input[name='tech1']").change(function () {
restrictCheckboxSeletions($(this));
});
$("input[name='tech2']").change(function () {
restrictCheckboxSeletions($(this));
});
$("input[name='tech3']").change(function () {
restrictCheckboxSeletions($(this));
});
function restrictCheckboxSeletions(checkbox) {
var countTech1 = $("input[name='tech1']:checked").length;
var countTech2 = $("input[name='tech2']:checked").length;
var countTech3 = $("input[name='tech3']:checked").length;
if (countTech1 > 0 && countTech2 > 0 && countTech3 > 0) {
checkbox.prop("checked", "");
alert('You can only select from 2 categories!');
} else {
var totalCount = countTech1 + countTech2 + countTech3;
if (totalCount > maxAllowedCheckboxes) {
checkbox.prop("checked", "");
alert('You can select maximum ' + maxAllowedCheckboxes + ' categories!');
}
}
}
});
$(document).ready(function () {
$("input[name='tech']").change(function () {
maxAllowed = 8;
var count = 1;
var first = $("input[name='tech']:checked:first").data("category");
var cnt = $("input[name='tech']:checked").length;
if (cnt > maxAllowed) {
$(this).prop("checked", "");
alert('You can select maximum ' + maxAllowed + ' categories!');
}
var cat = $("input[name='tech']:checked").each(function(index) {
if($(this).data('category') != first)
count ++;
if(count > 2)
alert("Can't have more than two categories");
});
});
});
最多在两个类别中选择多个复选框:
第一类
复选框
复选框
复选框
复选框
复选框
第2类
复选框
复选框
复选框
复选框
复选框
第3类
复选框
复选框
复选框
复选框
复选框
$(文档).ready(函数(){
var MaxAllowedCheckBox=8;
$(“输入[name='tech1']”)。更改(函数(){
限制复选框选项($(此));
});
$(“输入[name='tech2']”)。更改(函数(){
限制复选框选项($(此));
});
$(“输入[name='tech3']”)。更改(函数(){
限制复选框选项($(此));
});
功能限制复选框选择(复选框){
var countTech1=$(“输入[name='tech1']:选中”).length;
var countTech2=$(“输入[name='tech2']:选中”).length;
var countTech3=$(“输入[name='tech3']:选中”).length;
如果(countTech1>0&&countTech2>0&&countTech3>0){
复选框.prop(“选中”和“);
警报('您只能从2个类别中选择!');
}否则{
var totalCount=countTech1+countTech2+countTech3;
如果(totalCount>MaxAllowed复选框){
复选框.prop(“选中”和“);
警报('您可以选择最大'+MaxAllowedCheckBox+'类别!');
}
}
}
});
一种简单的方法如下:
<h3>Select multi checkbox inside 2 categories max:</h3>
<div class="col-xs-4">
<h5>Category 1</h5>
<input type="checkbox" name="tech1" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech1" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 2</h5>
<input type="checkbox" name="tech2" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech2" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 3</h5>
<input type="checkbox" name="tech3" value="jQuery" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Prototype" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Dojo" /> checkbox
<br/>
<input type="checkbox" name="tech3" value="Mootools" /> checkbox
<br/>
</div>
$(document).ready(function () {
var maxAllowedCheckboxes = 8;
$("input[name='tech1']").change(function () {
restrictCheckboxSeletions($(this));
});
$("input[name='tech2']").change(function () {
restrictCheckboxSeletions($(this));
});
$("input[name='tech3']").change(function () {
restrictCheckboxSeletions($(this));
});
function restrictCheckboxSeletions(checkbox) {
var countTech1 = $("input[name='tech1']:checked").length;
var countTech2 = $("input[name='tech2']:checked").length;
var countTech3 = $("input[name='tech3']:checked").length;
if (countTech1 > 0 && countTech2 > 0 && countTech3 > 0) {
checkbox.prop("checked", "");
alert('You can only select from 2 categories!');
} else {
var totalCount = countTech1 + countTech2 + countTech3;
if (totalCount > maxAllowedCheckboxes) {
checkbox.prop("checked", "");
alert('You can select maximum ' + maxAllowedCheckboxes + ' categories!');
}
}
}
});
$(document).ready(function () {
$("input[name='tech']").change(function () {
maxAllowed = 8;
var count = 1;
var first = $("input[name='tech']:checked:first").data("category");
var cnt = $("input[name='tech']:checked").length;
if (cnt > maxAllowed) {
$(this).prop("checked", "");
alert('You can select maximum ' + maxAllowed + ' categories!');
}
var cat = $("input[name='tech']:checked").each(function(index) {
if($(this).data('category') != first)
count ++;
if(count > 2)
alert("Can't have more than two categories");
});
});
});
最多在两个类别中选择多个复选框:
第一类
复选框
复选框
复选框
复选框
复选框
第2类
复选框
复选框
复选框
复选框
复选框
第3类
复选框
复选框
复选框
复选框
复选框
$(文档).ready(函数(){
var MaxAllowedCheckBox=8;
$(“输入[name='tech1']”)。更改(函数(){
限制复选框选项($(此));
});
$(“输入[name='tech2']”)。更改(函数(){
限制复选框选项($(此));
});
$(“输入[name='tech3']”)。更改(函数(){
限制复选框选项($(此));
});
功能限制复选框选择(复选框){
var countTech1=$(“输入[name='tech1']:选中”).length;
var countTech2=$(“输入[name='tech2']:选中”).length;
var countTech3=$(“输入[name='tech3']:选中”).length;
如果(countTech1>0&&countTech2>0&&countTech3>0){
复选框.prop(“选中”和“);
警报('您只能从2个类别中选择!');
}否则{
var totalCount=countTech1+countTech2+countTech3;
如果(totalCount>MaxAllowed复选框){
复选框.prop(“选中”和“);
警报('您可以选择最大'+MaxAllowedCheckBox+'类别!');
}
}
}
});
给每个组一个不同的属性,逻辑很容易遵循
$(文档).ready(函数(){
$(“输入[name='tech']”)。更改(函数(){
var maxAllowed=8;
var-maxgr=2;
var cnt=$(“输入[name='tech']:选中”).length;
var组=[];
$(“输入[name='tech']:选中”)。每个(函数(){
groups.push($(this.attr('group'));
如果($.unique(组).length>2){
$(this.prop(“选中”和“”);
警报('您可以选择最大'+maxgr+'组!');
}
});
如果(cnt>maxAllowed){
$(this.prop(“选中”和“”);
警报('您可以选择最大'+maxAllowed+'类别!');
}
});
});代码>
在最多两个类别内选择多个复选框:
第一类
复选框
复选框
复选框
复选框
复选框
第2类
复选框
复选框
复选框
复选框
复选框
第3类
复选框
复选框
复选框
复选框
复选框
给每个组一个不同的属性,逻辑很容易遵循
$(文档).ready(函数(){
$(“输入[name='tech']”)。更改(函数(){
var maxAllowed=8;
var-maxgr=2;
var cnt=$(“输入[name='tech']:选中”).length;
var组=[];
$(“输入[name='tech']:选中”)。每个(函数(){
groups.push($(this.attr('group'));
如果($.unique(组).length>2){
$(this.prop(“选中”和“”);
警报('您可以选择最大'+maxgr+'组!');
}
});
如果(cnt>maxAllowed){
$(this.prop(“选中”和“”);
警报('您可以选择最大'+maxAllowed+'类别!');
}
});
});代码>
在最多两个类别内选择多个复选框:
第一类
复选框
复选框
复选框
复选框
复选框
第2类
复选框
复选框
复选框
复选框
复选框
第3类
复选框
复选框
复选框
复选框
复选框
我已经在html输入中包含了数据属性
<h3>Select multi checkbox inside 2 categories max:</h3>
<div class="col-xs-4">
<h5>Category 1</h5>
<input type="checkbox" data-category="Category1" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 2</h5>
<input type="checkbox" data-category="Category2" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 3</h5>
<input type="checkbox" data-category="Category3" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Mootools" /> checkbox
<br/>
</div>
希望有帮助。我已经在html输入中包含了数据属性
<h3>Select multi checkbox inside 2 categories max:</h3>
<div class="col-xs-4">
<h5>Category 1</h5>
<input type="checkbox" data-category="Category1" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category1" name="tech" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 2</h5>
<input type="checkbox" data-category="Category2" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category2" name="tech" value="Mootools" /> checkbox
<br/>
</div>
<div class="col-xs-4">
<h5>Category 3</h5>
<input type="checkbox" data-category="Category3" name="tech" value="jQuery" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="JavaScript" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Prototype" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Dojo" /> checkbox
<br/>
<input type="checkbox" data-category="Category3" name="tech" value="Mootools" /> checkbox
<br/>
</div>
希望能有帮助