我有一个包含价格值的X个复选框组的表单。 我为每个小组分配了一个条件。
让我们假设我们有两组:
第一组条件:将所有值加在一起,除了前面选择的3个 (不是索引中的前3个,而是前3个选中,之后的每个选择)
第二组条件:将所有值加在一起,除了前两个选择的值 (不是索引中的前2个,而是前2个被选中,之后的每个选择)
var minLimit1 = 3;
var minLimit2 = 2;
$(":input", "#myForm").each(function(){
if(this.type == 'checkbox'){
}
});
如何使用jQuery或plain js实现它?
编辑:样本表格代码
<form id="myForm">
<fieldset class="required">
<input type="checkbox" value="1.30" name="group1" id="id-1">
<label for="id-1"><span>Component 1</span><em>(€1.30)</em></label>
<input type="checkbox" value="1.00" name="group1" id="id-2">
<label for="id-2"><span>Component 2</span><em>(€1.00)</em></label>
<input type="checkbox" value="0.50" name="group1" id="id-3">
<label for="id-3"><span>Component 3</span><em>(€0.50)</em></label>
<input type="checkbox" value="0.25" name="group1" id="id-4">
<label for="id-4"><span>Component 4</span><em>(€0.25)</em></label>
<input type="checkbox" value="1.75" name="group1" id="id-5">
<label for="id-5"><span>Component 5</span><em>(€1.75)</em></label>
<input type="checkbox" value="2.00" name="group1" id="id-6">
<label for="id-6"><span>Component 6</span><em>(€2.00)</em></label>
</fieldset>
<fieldset class="required">
<input type="checkbox" value="1.20" name="group2" id="id-7">
<label for="id-7"><span>Component 1</span><em>(€1.20)</em></label>
<input type="checkbox" value="1.10" name="group2" id="id-8">
<label for="id-8"><span>Component 2</span><em>(€1.10)</em></label>
<input type="checkbox" value="0.40" name="group2" id="id-9">
<label for="id-9"><span>Component 3</span><em>(€0.40)</em></label>
<input type="checkbox" value="0.35" name="group2" id="id-10">
<label for="id-10"><span>Component 4</span><em>(€0.35)</em></label>
<input type="checkbox" value="1.15" name="group2" id="id-11">
<label for="id-11"><span>Component 5</span><em>(€1.15)</em></label>
</fieldset>
</form>
注意:我希望最新点击并进一步添加到摘要中,而不是索引中的最后一个。
答案 0 :(得分:1)
我认为这个解决方案可以满足您的需求。 Demo将类添加到计算值的标签中,以便更容易确定概念是否正确
DEMO:http://jsfiddle.net/MTJtF/2
/* can store values in array, or as data attribute of html */
var min_limits = [3, 2];
$('#myForm .required input:checkbox').change(function() {
var $group = $(this).closest('fieldset.required')
var parentIdx = $('fieldset.required').index($group);
/* can store values in array, or as data attribute of html */
var minLimit = $group.data('min_limit') || min_limits[parentIdx];
var $selected = $group.find(':checked').slice(minLimit);
$group.find('.counted').removeClass('counted')
var sum = 0;
if ($selected.length) {
$selected.each(function() {
sum +=1*$(this).val();
$(this).next().addClass('counted')
})
} else {
sum = 'Limit not met';
}
$group.find('.sum').text('Sum= '+sum)
})
答案 1 :(得分:0)
传递给$.each()
的第一个参数是index
,这在这里会有所帮助。
在此代码中,我们假设您的复选框按fieldset
元素分组,其中包含"group1"
,"group2"
等等。
var conditions = {
group1: 3,
group2: 2
},
total = 0,
key, condition;
// Loop through all the conditions
for (key in conditions) {
condition = conditions[key];
// Loop through all the checked checkboxes in fieldset.group1, fieldset.group2, etc
$('input[type="checkbox"]:checked', '#myForm fieldset.' + key).each(function(inputIndex) {
// Check if the current checkbox's index is higher than the minimum
if ((inputIndex + 1) > condition) {
total += parseFloat($(this).val());
}
});
}
示例:http://jsfiddle.net/QfSxw/2/
编辑:大大简化了代码
答案 2 :(得分:0)
我不确定我是否正确理解这些复选框的格式,但我假设您可以区分组1复选框和组2复选框。如果是这样,我建议跟踪到目前为止已标记的复选框的数量,以了解何时开始汇总值。例如:
// The minimum number that must be checked
var minLimit1 = 3;
var minLimit2 = 2;
// The number checked per group
var numGroup1 = 0;
var numGroup2 = 0;
//The sum of the checkboxes over the group's minLimit that are checked
var sumGroup1 = 0;
var sumGroup2 = 0;
$(":input", "#myForm").each(function () {
if ((this.type == 'checkbox') && (this.checked == true)) {
// This code is just for Group 1 right now. Depending on how you differentiate
// between each group, you could either have a check in here for each group
// (or some kind of loop if the number of groups is variable), or have a separate
// each statement for each group.
if (numGroup1 >= minLimit1) {
// If the minLimit has already been reached, then increment the sum
sumGroup1++;
}
numGroup1++;
}
});