jquery - jquery - 计算使用 checkAll 按钮选中的所有复选框
问题描述
我有一个脚本,可以将 div 中所有选中的复选框值与 class="show_list_item" 相加。它工作正常。
我还有一个脚本,允许用户一键检查所有复选框(id="checkAll")。它也运行良好。
当用户使用“checkall”按钮时,脚本不再计算选中/选中的值。
我该如何解决?
查询
// CALCULATE TOTAL CHECKED
$('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 : Number ($(this).val());
});
$("#total_sum").text(total_srvs_amount);
});
// CHECK ALL CHECKBOXS
$(function () {
var chkMain = $('.checkAll input:checkbox#checkAll , .checkAll input:checkbox.checkAllBtn');
$(chkMain).change(function () {
$('.checkAll input:checkbox').not(this).prop('checked', this.checked);
});
});
HTML
<div id="1" class="obj_res_box show_list_item">
<input type="checkbox" class="" value="" id="checkAll">
<table class="checkAll">
<tr>
<td><input type="checkbox" name="amount" value="138.75"></td>
<td><input type="checkbox" name="amount" value="120"></td>
<td><input type="checkbox" name="amount" value="64"></td>
</tr>
</table>
</div>
<div id="2" class="obj_res_box hide_list_item">
....
</div>
<div id="total_sum">0</div>
解决方案
// CHECK ALL CHECKBOXS 函数有错误。
我已经纠正了。请查阅
// CALCULATE TOTAL CHECKED
$('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 : Number ($(this).val());
});
$("#total_sum").text(total_srvs_amount);
});
// CHECK ALL CHECKBOXS
$(function () {
var chkMain = $('input:checkbox#checkAll');
$(chkMain).change(function () {
$('.checkAll input:checkbox').prop('checked', this.checked);
$('.checkAll input:checkbox').trigger('change');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="1" class="obj_res_box show_list_item">
<input type="checkbox" class="" value="" id="checkAll">
<table class="checkAll">
<tr>
<td><input type="checkbox" name="amount" value="138.75"></td>
<td><input type="checkbox" name="amount" value="120"></td>
<td><input type="checkbox" name="amount" value="64"></td>
</tr>
</table>
</div>
<div id="2" class="obj_res_box hide_list_item">
....
</div>
<div id="total_sum">0</div>
推荐阅读
- ajax - 使用 AJAX 的 Django POST 不起作用
- python - 许多图像的 tesseract 速度改进
- amazon-web-services - 将 S3 存储桶数据实时移动到另一个存储桶 - AWS Lambda?
- java - Android Studio 基于令牌的身份验证 - 基于日期
- linux - 如何解决rpm不可读错误
- excel - 使用循环设置范围
- javascript - WebGL:使用两个着色器,第一个输出作为第二个的输入,只看到第二个的输出
- javascript - 解决错误:passport.initialize() 中间件未使用
- azure - 是否可以对 CosmosDB 集合使用继承?
- ios - IOS中第三方应用程序的自动化