在这里,我有一堆复选框在他们各自的div中。
每个div将包含一个'data-min‘(ex: data-min="2")值,以限制用户选中div中的最小复选框no。
演示:小提琴
<select id="count">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<br/>
<br/>
<div class="checkbox" data-toggle="false" data-min="2" style='float:left;background:yellow;width:100px'>
<input id="checkbox-1" type="checkbox" name="Data1" value="option1" />
<label for="checkbox-1">HTML</label>
<br />
<input id="checkbox-2" type="checkbox" name="Data2" value="option2" />
<label for="checkbox-2">CSS</label>
<br />
<input id="checkbox-3" type="checkbox" name="Data3" value="option3" />
<label for="checkbox-3">HTML</label>
<br />
</div>
<div class="checkbox" data-toggle="false" data-min="2" style='float:left;margin-left:100px;background:brown;width:100px'>
<input id="checkbox-4" type="checkbox" name="Data4" value="option4" />
<label for="checkbox-4">CSS</label>
<br />
<input id="checkbox-5" type="checkbox" name="Data5" value="option5" />
<label for="checkbox-5">HTML</label>
<br />
<input id="checkbox-6" type="checkbox" name="Data6" value="option6" />
<label for="checkbox-6">CSS</label>
</div>jQuery
$(document).ready(function () {
$('#count').on('change', function () {
$('input[type=checkbox]').prop('checked', false);
$('[data-toggle]').data('toggle', false);
});
$('input[type=checkbox]').on('change', function () {
if ($('input[type=checkbox]:checked').length > $('#count').val()) {
$(this).prop('checked', false);
}
});
$('.checkbox').on('click', function (e) {
var cnt = $('input[type="checkbox"]:checked').length;
var cntSel = $('select').val();
var fin = cntSel - cnt;
var min = $('.checkbox').data('min');
if (e.target.className == "checkbox") {
if ($(this).data('toggle') == false) {
$(this).data('toggle', true);
$(this).find('input[type="checkbox"]:lt(' + fin + ')').prop('checked', true);
} else {
$(this).data('toggle', false);
$(this).find('input[type="checkbox"]').prop('checked', false);
}
}
});
});我该怎么做有人能帮我吗。
Edit1:
例如,
发布于 2013-09-10 06:57:02
不完整,只是第一想法http://jsfiddle.net/HNmhL/35/注意:$(‘checkboxId’).prop(‘复选’,假)可能打破复选框,不知道如何,但帕夫洛说
$(document).ready(function () {
$('#count').on('change', function(){
//do stuff here
//my random "on change" behaviour {
var chkboxes = $('input[type=checkbox]:checked');
for(var i = 0; i < chkboxes.length; i++){
$(chkboxes[i]).prop('checked', false);
}
//end of my random "on change" bahaviour}
});
$('input[type=checkbox]').on('change', function(e){
//this was deselected no need to check?
if(!$(this).prop('checked')){
return false;
}
//checked count exceeded #count value
if($('input[type=checkbox]:checked').length > $('#count').val()){
$(this).prop('checked', false);
alert('Count of checked checkboxes >= #count');
return false;
}
//if checked check boxes count = #count, validate all divs
if($('input[type=checkbox]:checked').length == $('#count').val()){
validateDivs();
}
var checksLeft = parseInt($('#count').val()) - $('input[type=checkbox]:checked').length;
var parent = $($(this).parent()[0]);
var parentSub = parent.find('input[type=checkbox]:checked').length; //count of checked checkboxes in current parent
if(parseInt(parent.attr('data-min')) - parentSub > checksLeft){
$(this).prop('checked', false);
alert('Not enought checks left');
return false;
}
});
});
function validateDivs(){
var chkboxes = $('.checkbox');
for(var i = 0; i < chkboxes.length; i++){
var chks = $(chkboxes[i]).find('input[type=checkbox]:checked');
if(chks.length < $(chkboxes[i]).attr('data-min')){
chks.each(function(index, checkbox){
$(checkbox).prop('checked', false);
})
}
}
}发布于 2013-09-10 08:17:06
经过一番努力,我终于得到了答案
小提琴
jQuery
$(document).ready(function () {
$('#count').on('change', function () {
//do stuff here
//my random "on change" behaviour {
var chkboxes = $('input[type=checkbox]:checked');
for (var i = 0; i < chkboxes.length; i++) {
$(chkboxes[i]).prop('checked', false);
}
//end of my random "on change" bahaviour}
});
$('input[type=checkbox]').on('change', function (e) {
//this was deselected no need to check?
if (!$(this).prop('checked')) {
return false;
}
//checked count exceeded #count value
if ($('input[type=checkbox]:checked').length > $('#count').val()) {
$(this).prop('checked', false);
alert('Count of checked checkboxes >= #count');
return false;
}
//if checked check boxes count = #count, validate all divs
if ($('input[type=checkbox]:checked').length == $('#count').val()) {
validateDivs();
}
var checksLeft = parseInt($('#count').val()) - $('input[type=checkbox]:checked').length;
var parent = $($(this).parent()[0]);
var parentSub = parent.find('input[type=checkbox]:checked').length; //count of checked checkboxes in current parent
if (parseInt(parent.attr('data-min')) - parentSub > checksLeft) {
$(this).prop('checked', false);
alert('Not enought checks left');
return false;
}
});
$('.checkbox').on('click', function (e) {
var cnt = $('input[type="checkbox"]:checked').length;
var cntSel = $('select').val();
var fin = cntSel - cnt;
var min = $(this).data('min');
if (e.target.className == "checkbox") {
if ($(this).data('toggle') == false) {
$(this).data('toggle', true);
if (fin < min) {
$(this).prop('checked', false);
alert("Minimun capacity of this table is: " + min + ".");
} else {
$(this).find('input[type="checkbox"]:lt(' + fin + ')').prop('checked', true);
}
} else {
$(this).data('toggle', false);
$(this).find('input[type="checkbox"]').prop('checked', false);
}
}
});
});
function validateDivs() {
var chkboxes = $('.checkbox');
for (var i = 0; i < chkboxes.length; i++) {
var chks = $(chkboxes[i]).find('input[type=checkbox]:checked');
if (chks.length < $(chkboxes[i]).attr('data-min')) {
chks.each(function (index, checkbox) {
$(checkbox).prop('checked', false);
})
}
}
}谢谢大家,支持我解决这个问题。
发布于 2013-09-10 05:21:40
我没有回答你的相似问题吗?无论如何,请查看这个小提琴示例。
$('input[type=checkbox]').on('click', function(event){
if($('div.checkbox input[type=checkbox]:checked').length > $('#count').val())
{
event.preventDefault();
}
});https://stackoverflow.com/questions/18711072
复制相似问题