简体   繁体   English

JQuery - 根据父类求和复选框值

[英]JQuery - sum check boxes values according parent class

I have script that sum all checked checkboxs, but I would like to sum only checkboxs values where parent div has class="show_list_item"我有对所有选中的复选框求和的脚本,但我只想对父 div 具有 class="show_list_item" 的复选框值求和

 $('input:checkbox').change(function (){ var total_srvs_amount = 0; $('input[name="amount"]:checkbox:checked').each(function(){ total_srvs_amount += isNaN($(this).val()) ? 0 : parseInt ($(this).val()); console.log(total_srvs_amount); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="show_list_item"> <input type="checkbox" name="amount" value="100"> <input type="checkbox" name="amount" value="120"> </div> <div class="hide_list_item"> <input type="checkbox" name="amount" value="85"> <input type="checkbox" name="amount" value="90"> </div>

You can change the selector to apply the function on checkboxes inside .show_list_item :您可以更改选择器以在.show_list_item内的复选框上应用该功能:

 $('.show_list_item input:checkbox').change(function () { var total_srvs_amount = 0; $('.show_list_item input[name="amount"]:checkbox:checked').each(function(){ total_srvs_amount += isNaN($(this).val()) ? 0 : parseInt ($(this).val()); console.log(total_srvs_amount); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="show_list_item"> <input type="checkbox" name="amount" value="100"> <input type="checkbox" name="amount" value="120"> </div> <div class="hide_list_item"> <input type="checkbox" name="amount" value="85"> <input type="checkbox" name="amount" value="90"> </div>

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

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