我有3种不同的表单,每个复选框的数量不同,每个部分基本上代表一个表单,所以当使用中选择该部分的复选框时,它会显示他们检查了该部分总金额的数量
HTML
<body class="container">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a class="btn1" role="button" data-toggle="collapse" data-parent="" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<p class="count-checked-checkboxes">Name of Section 0</p>
<p class="numOfQuestions"></p>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<form id="form1" action="">
<input id="question-1" type="checkbox">
<label for="question-1">solution name</label>
<br>
<input id="description" type="checkbox">
<label for="description">solution name</label>
<br>
<input id="question-2" type="checkbox">
<label for="question-2">demo question</label>
<br>
<input id="question-3" type="checkbox">
<label for="question-3">another demo question</label>
</form>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed btn1" role="button" data-toggle="collapse" data-parent="" href="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo">
<p class="count-checked-checkboxes">Name of Section 0</p>
<p class="numOfQuestions"></p>
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
<form id="form2" action="">
<input id="ques-1" type="checkbox">
<label for="ques-1">solution name</label>
<br>
<input id="description1" type="checkbox">
<label for="description1">solution name</label>
<br>
<input id="ques2" type="checkbox">
<label for="ques2">demo question</label>
<br>
<input id="ques3" type="checkbox">
<label for="ques3">another demo question</label>
<br>
<input id="ques11" type="checkbox">
<label for="ques11">another demo question</label>
</form>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingThree">
<h4 class="panel-title">
<a class="collapsed btn1" role="button" data-toggle="collapse" data-parent="" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">
<p class="count-checked-checkboxes">Name of Section 0</p>
<p class="numOfQuestions"></p>
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
<div class="panel-body">
<form id="form3" action="">
<input id="ques4" type="checkbox">
<label for="ques4">solution name</label>
<br>
<input id="description3" type="checkbox">
<label for="description3">solution name</label>
<br>
<input id="ques5" type="checkbox">
<label for="ques5">demo question</label>
<br>
<input id="ques6" type="checkbox">
<label for="ques6">another demo question</label>
<br>
<input id="ques44" type="checkbox">
<label for="ques44">another demo question</label>
<br>
<input id="ques64" type="checkbox">
<label for="ques64">another demo question</label>
</form>
</div>
</div>
</div>
</div>
</body>
jquery的
$(function(){
var e = document.forms[0];
var numQuestion;
var i;
var formName = $(this).e;
for(i = 0; i < e.length; i++){
numQuestion = e.getEle**mentsByTagName('label');
console.log(numQuestion.length);
}
$('#' + formName).closest('.panel-heading').find('.numOfQuestions').text('/' + e.length);
var $checkboxes = $('input[type="checkbox"], #form1');
$checkboxes.change(function(){
var countCheckedCheckboxes = $checkboxes.filter(':checked').length;
$(this).closest('.panel-heading').find('.count-checked-checkboxes').text('Name of Section ' + countCheckedCheckboxes);
});
});
答案 0 :(得分:0)
将更改事件添加到每个输入复选框,然后在更改事件中,找到最接近的<TomEE>/lib
然后.pannel-body
,并将长度分配给find('input:checked')
。
接下来只需更新(使用最近的查找.pannel)并更新文本
注意:最近的将会找到父母,它将找不到兄弟或孩子。
count
&#13;
$('input[type="checkbox"]').on('change', function() {
var count = $(this).closest('.panel-body').find('input:checked').length;
$(this).closest('.panel').find('.count-checked-checkboxes').text(count);
updateTotal();
});
function updateTotal(){
var total = 0;
$('.count-checked-checkboxes').each(function(){
total += +$(this).text();
});
$('#mytotal').text(total);
}
&#13;
答案 1 :(得分:0)
选择器可能是问题
{{1}}
尝试使用parent()或nearest()
答案 2 :(得分:0)
这就是我提出的工作。为清楚起见,我认为您可以按照它进行必要的修改。
// --- when user changes a checkbox ---
$(document).on('change', 'input[type="checkbox"]', function(e) {
// --- get the form name ---
var $form = $(this).closest('form');
var formName = $form.attr('id');
// --- count the checkboxes for this form ---
var totalCheckboxes = $form.children('input[type="checkbox"]').length;
// --- place count value in the 'numOfQuestions' p ---
var $panel = $form.closest('.panel-default');
$panel.find('.numOfQuestions').text('/' + totalCheckboxes);
// --- count total checkboxes checked ---
var countCheckedCheckboxes = $form.children('input[type="checkbox"]:checked').length;
// --- place checked count in 'count-checked-checkboxes' p ---
$panel.find('.count-checked-checkboxes').text('Name of Section '+countCheckedCheckboxes);
});