gpt4 book ai didi

javascript - Accordion 式 list 计数器 document.form[]

转载 作者:行者123 更新时间:2023-11-28 04:34:57 33 4
gpt4 key购买 nike

我有 3 种不同的表单,其中复选框数量不同,每个部分基本上代表一个表单,因此当用户选择该部分中的复选框时,它会显示他们在该部分的总金额中 checkout 了多少

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

});

my progress so far

最佳答案

这就是我想出的可以完成这项工作的方法。为了清晰起见,我认为您可以按照它进行任何必要的修改。

// --- 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);

});

关于javascript - Accordion 式 list 计数器 document.form[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291153/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com