简体   繁体   English

复选框值的总和

[英]Total sum of checkbox values

I have a simple checkbox code, 2 checkboxes per group. 我有一个简单的复选框代码,每个组2个复选框。

    <div class='product-addon'>
        <p>Interfejsy</p>
        <label>
            <input id='option0' type='checkbox' name='Interfejsy' value='150.00'>
            Moduł rozszerzenia 1: 2x RS232 <span class='addon-price'>150,00</span>
        </label>
        <label>
            <input id='option1' type='checkbox' name='Interfejsy' value='370.67'>
            Moduł rozszerzenia 2: WiFi <span class='addon-price'>370,67</span>
        </label>
    </div>
    <div class='product-addon'>
        <p>BAZA</p>
        <label>
            <input id='option0' type='checkbox' name='BAZA' value='60.00'>
            Rozszerzenie bazy PLU o 2000 kodów  <span class='addon-price'>60,00</span>
        </label>
        <label>
            <input id='option1' type='checkbox' name='BAZA' value='80.00'>
            Rozszerzenie bazy PLU o 4000 kodów  <span class='addon-price'>80,00</span>
        </label>
    </div>

My question is: How i can get total sum of price (checkbox.val()) when user click checkbox? 我的问题是:当用户单击复选框时,如何获取总价(checkbox.val())? For example user click checkbox in first group with value=150.00, and click checkbox in second group with value=80.00? 例如,用户单击第一组中值为150.00的复选框,然后单击第二组中值为80.00的复选框? Total sum should be 230.00 Important: user can select only one checkbox in group. 总金额应为230.00重要说明:用户只能在组中选择一个复选框。

I have that js code: 我有那个js代码:

    var totalSum = 0;
    $("input:checkbox").on('click', function() {
      var $box = $(this);

      if ($box.is(":checked")) {
        var group = "input:checkbox[name='" + $box.attr("name") + "']";
        var addonPrice = parseFloat($box.val());
        $(group).prop("checked", false);
        $box.prop("checked", true);
      } else {
        $box.prop("checked", false);
      }

      totalSum += addonPrice;
      console.log(totalSum);
    });

but the variable totalSum is always getting higher, when i click any addon. 但是当我单击任何插件时,变量totalSum总是越来越高。 For example i click checkbox in group 1 with value=150, next click checkbox in group 2 with value=60, totalSum = 210 it's good, but when i click again some checkbox it's adding next value. 例如,我单击组1中值= 150的复选框,然后单击组2中值= 60,totalSum = 210的复选框,这很好,但是当我再次单击某些复选框时,它将添加下一个值。

I just wan't something like this: 1) User click checkbox in group 1 with value=150 2) User click checkbox in group 2 with value=60 3) totalSum is 210 4) User click checkbox in group 2 with value=80 5) totalSum is 230, not 290 我只是不想这样:1)用户单击组1中值为= 150的复选框2)用户单击组2中值为= 60的复选框3)totalSum为210 4)用户单击组2中值为80的复选框5)totalSum是230,而不是290

Thanks for any suggestions 谢谢你的建议

You don't subtract the value of the changed input if it was unchecked. 如果未选中,则不要减去更改后的输入的值。

Another way of doing this is to just select all the checked inputs and sum their values. 这样做的另一种方法是只选择所有检查的输入并对它们的值求和。

Something like this: 像这样:

 $("input:checkbox").change(function(e){ if($(this).is(":checked")){ $(`[name=${this.name}]`).not($(this)).prop("checked", false); } var total = $.makeArray($("input:checkbox:checked").map(function(){ return parseInt($(this).val()); })).reduce((a, c) => (a+c), 0) console.log(total) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='product-addon'> <p>Interfejsy</p> <label> <input id='option0' type='checkbox' name='Interfejsy' value='150.00'> Moduł rozszerzenia 1: 2x RS232 <span class='addon-price'>150,00</span> </label> <label> <input id='option1' type='checkbox' name='Interfejsy' value='370.67'> Moduł rozszerzenia 2: WiFi <span class='addon-price'>370,67</span> </label> </div> <div class='product-addon'> <p>BAZA</p> <label> <input id='option0' type='checkbox' name='BAZA' value='60.00'> Rozszerzenie bazy PLU o 2000 kodów <span class='addon-price'>60,00</span> </label> <label> <input id='option1' type='checkbox' name='BAZA' value='80.00'> Rozszerzenie bazy PLU o 4000 kodów <span class='addon-price'>80,00</span> </label> </div> 

Radio buttons will be more appropriate for what you're trying to do instead of checkboxes. 单选按钮将比复选框更适合您要执行的操作。

Your main problem is that you are only initializing the totalSum once , outside of your callback function, so no matter how many clicks happen, the total is never reset. 您的主要问题是,您只需在回调函数之外初始化一次totalSum ,因此无论发生了多少点击,都不会重置总数。 That has to be moved into the function so that you start off with a total of 0 and get the total upon each successive click. 必须将其移到函数中,以便您以0总数开始,并在每次连续单击时获得总数。

Now, based on what you are doing with the toggling of the checked status of the checkboxes, it's clear that you should be using radio buttons, not checkboxes . 现在,根据您对复选框的已选中状态的切换所进行的操作,很明显, 您应该使用单选按钮,而不是复选框 And, making that change makes the solution much simpler. 并且,进行更改使解决方案变得更加简单。

See comments inline below: 请参阅下面的内联评论:

 $("input:radio").on('click', function() { var totalSum = 0; // Start getting a new total on each new click // Just loop over only the checked radio buttons $("input[type='radio']:checked").each(function(index, element){ totalSum += +$(element).val(); // Add up their values }); console.clear(); console.log(totalSum.toFixed(2)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='product-addon'> <p>Interfejsy</p> <label> <input id='option0' type='radio' name='Interfejsy' value='150.00'> Moduł rozszerzenia 1: 2x RS232 <span class='addon-price'>150,00</span> </label> <label> <input id='option1' type='radio' name='Interfejsy' value='370.67'> Moduł rozszerzenia 2: WiFi <span class='addon-price'>370,67</span> </label> </div> <div class='product-addon'> <p>BAZA</p> <label> <input id='option0' type='radio' name='BAZA' value='60.00'> Rozszerzenie bazy PLU o 2000 kodów <span class='addon-price'>60,00</span> </label> <label> <input id='option1' type='radio' name='BAZA' value='80.00'> Rozszerzenie bazy PLU o 4000 kodów <span class='addon-price'>80,00</span> </label> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM