0

如何使用名称从所有输入中添加值name="TotalInline[]"

以下内容不起作用:

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

5 回答 5

4

这应该工作:

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); 
});
于 2012-09-11T18:24:18.560 回答
3
var total = 0;
$.each($('input[name="TotalInline[]"]'), function() {
    total += parseInt(this.value, 10);
});
于 2012-09-11T18:24:27.620 回答
2

你有一些严重的语法错误,试试这个:

var total = 0;
$('input[name="TotalInline[]"]').each(function () {
  total += parseInt(this.value, 10);
});
于 2012-09-11T18:24:35.143 回答
1

试试这样...

var total = 0;
$('input[name="TotalInline[]"]').each(function() {
        total += parseInt($(this).val(),10);
    });
于 2012-09-11T18:25:47.220 回答
1
var total = 0;

$('input[name="TotalInline[]"]').each(function() {
    total += +this.value.replace(/[^\d.]/g, '');
});
  • 使用快速正则表达式仅过滤掉数字(和小数点)。
  • 使用+前缀转换为数字。
于 2012-09-11T18:27:01.087 回答