0

我有一个带有 X 组带有价格值的复选框的表单。我为每个组分配了一个条件。

假设我们有 2 个组:

第一组条件:将所有值加在一起,除了选择的前 3 个(不是索引中的前 3 个,而是选择的前 3 个以及之后的每个选择)

第二组条件:将所有值加在一起,除了前 2 个被选中(不是索引中的前 2 个,而是前 2 个被选中,之后的每个选择)

var minLimit1 = 3;
var minLimit2 = 2;

$(":input", "#myForm").each(function(){

    if(this.type == 'checkbox'){



    }

});

如何使用 jQuery 或纯 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>

注意:我希望将最新点击并进一步添加到摘要中,而不是索引中的最后一个。

4

3 回答 3

1

我认为这个解决方案可以满足您的需求。演示将类添加到正在计算的值的标签中,以便更容易确定该概念是否正确

演示: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)

})

​</p>

于 2012-10-20T20:06:47.590 回答
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/

编辑:大大简化了代码

于 2012-10-20T13:01:13.453 回答
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++;
    }
});
于 2012-10-20T13:02:10.223 回答