将输入名称为“”的所有值相加;TotalInline[]”;

Add all values with input name="TotalInline[]"

本文关键字:TotalInline 输入      更新时间:2023-09-26

如何从名称为name="TotalInline[]"的所有输入中添加值?

以下内容不起作用:

    var total = 0;
    $.each('input[name="TotalInline[]"];,function() {
        total += this;
    });

这应该有效:

var total = 0;
$('input[name="TotalInline"]').each(function() {
    // assuming you have ints in your inputs, use parseFloat if those are floats
    total += parseInt(this.value, 10); 
});
var total = 0;
$.each($('input[name="TotalInline[]"]'), function() {
    total += parseInt(this.value, 10);
});

您有一些严重的语法错误,请尝试以下操作:

var total = 0;
$('input[name="TotalInline[]"]').each(function () {
  total += parseInt(this.value, 10);
});

这样尝试。。。

var total = 0;
$('input[name="TotalInline[]"]').each(function() {
        total += parseInt($(this).val(),10);
    });
var total = 0;
$('input[name="TotalInline[]"]').each(function() {
    total += +this.value.replace(/[^'d.]/g, '');
});
  • 使用快速正则表达式只过滤掉数字(和小数点)
  • 使用+前缀转换为数字