简体   繁体   English

jQuery-将多个值加在一起是复选框

[英]jQuery - Adding multiple values together is checkbox is checked

I have a quote form on a page (made using Gravity Forms WP plugin, but that doesn't matter). 我在页面上有一个报价表(使用Gravity Forms WP插件制作,但这没关系)。 The form has multiple checkboxes on it - call these options. 表单上有多个复选框-调用这些选项。 Each option has a different value 'assigned' to it. 每个选项都有一个“分配”的不同值。 What I want to do, is add these values together if that particular checkbox is ticked, and subtract it's value if it's not. 我想做的是,如果选中了该特定复选框,则将这些值加在一起,如果没有,则减去它们的值。 I hope that makes sense...? 我希望这是有道理的...?

Here is what I have so far: 这是我到目前为止的内容:

jQuery(document).ready( function() {
    jQuery('#choice_2_10_4, #choice_2_10_5').change( function() {
        var option1 = Number( jQuery('#choice_2_10_4:checked').val() || 0 ); // Add 250 if checked
        var option2 = Number( jQuery('#choice_2_10_5:checked').val() || 0 ); // Add 200 if checked
        jQuery('#quote-price').val( option1 + option2 );
    });
});

#quote-price is the field I'd like to show the total of the calculation in. #quote-price是我要在其中显示计算总数的字段。

Can i give #choice_2_10_4 and #choice_2_10_5 a value by changing them to: 我可以通过将它们更改为来为#choice_2_10_4#choice_2_10_5一个值:

jQuery('#choice_2_10_4:checked').val(250)
jQuery('#choice_2_10_5:checked').val(250)

Or something along those lines? 或类似的规定?

Thanks 谢谢

Yes, you can set the value but after that you have to trigger the change event like so: 是的,您可以设置该值,但之后必须触发更改事件,如下所示:

jQuery('#choice_2_10_4:checked').val(250).trigger('change');
jQuery('#choice_2_10_5:checked').val(250).trigger('change');

 $(function() { $('.choice').on('change', function() { var total = 0; $('.choice').each(function() { total += this.checked ? +this.value : -this.value; }); $('#quote-price').val( total ); }) .change(); $('.add').on('click', function() { var target = $(this).data('target'), theval = $(target).is(':checked') ? +$(this).data('value') : +$(target).val(); $(target).val( theval ).trigger('change'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="choice_2_10_4" class="choice" value="200"> 200 <button type="button" data-target="#choice_2_10_4" data-value="250" class="add">Add 250</button><br> <input type="checkbox" id="choice_2_10_5" class="choice" value="400"> 400 <button type="button" data-target="#choice_2_10_5" data-value="350" class="add">Add 350</button><br> <input type="text" readonly="readonly" id="quote-price"/> 

Instead of assigning a value to the checkboxes via #choice_2_10_4:checked').val(250) you can just put the value into the checkbox in the first place eg <input type="checkbox" value="250" /> 无需通过#choice_2_10_4:checked').val(250)为复选框分配值,您只需将值首先放在复选框中即可,例如<input type="checkbox" value="250" />

Then just call it via #choice_2_10_4:checked').val() 然后通过#choice_2_10_4:checked').val()调用它

When you call #choice_2_10_4:checked').val(250) it returns an array of elements. 当您调用#choice_2_10_4:checked').val(250)它将返回一个元素数组。

console.log() it to see it for yourself console.log()亲自查看

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

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