简体   繁体   English

如何在JavaScript中添加多个值?

[英]how to add multiple values in javascript?

i have this situation: 我有这种情况:

...
 for (var i = 0; i < json.length; i++)
 {
  output += '<tr>';
     for ( var objects in json[i])
     {
        if (objects == 'amt_1')
       {
         output += '<td id="amt">' + json[i][objects] + '</td>';
       }
      }
  output += '</tr>';
        }
     output += '<tr">';
     var amt = 0;
     $('#amt').each(function() {
        amt += $(this).text();
     });
     output += '<td>' + amt + '</td>';
     output += '</tr>';
  $('#details').append(output);

 }

this is a part of a table that give's me something like this: 这是表的一部分,给我这样的东西:

<td id="amt">4.58</td>
<td id="amt">55.74</td>
<td id="amt">193.5</td>
<td></td>

and in the last td i would like the sum of the rest of them with the id = amt what i have seems not to work 在最后一个td我想用id = amt其余的总和,我似乎不起作用

any ideas? 有任何想法吗?

Thnaks 恶作剧

The problem is that you are using id's instead of classes, id's are supposed to be unique, so javascript only returns 1 td. 问题是您使用的是id而不是类,id应该是唯一的,因此javascript仅返回1 td。 Multiple elements however, can share the same class. 但是,多个元素可以共享同一类。

Also, the jQuery won't work because the elements haven't been added to the document yet. 而且,jQuery不会起作用,因为尚未将元素添加到文档中。

for (var i = 0; i < json.length; i++)
{
     output += '<tr>';
     for ( var objects in json[i])
     {
         if (objects == 'amt_1')
         {
             output += '<td class="amt">' + json[i][objects] + '</td>';
         }
      }
      output += '</tr>';
}
output += '<tr">';
$('#details').append(output); //Copied here, this will add the above elements 
                              //to the document subtree, which allows jquery 
                              //to search for them
output = ""; //Reset output
var amt = 0;
$('.amt').each(function() { //Changed to class selector
    amt += parseInt($(this).text());
});
output += '<td>' + amt + '</td>';
output += '</tr>';
$('#details').append(output); //Append result

Like they said you should use a class instead of id 就像他们说的那样,您应该使用类而不是ID

output += '<td class="amt">' + json[i][objects] + '</td>';

Then you must add the output to the DOM before your jQuery selector, $('.amt') , can find the elements 然后,必须 jQuery选择器$('。amt')找到元素之前将输出添加到DOM

$('#details').append(output);  //<--append BEFORE you select the .amt elements
 var amt = 0;
 $('.amt').each(function() {
    amt += parseFloat($(this).html());  //parseFloat on the html
 });
output = '<tr">';  //<-- reset output here
 output += '<td>' + amt + '</td>';
 output += '</tr>';
$('#details').append(output);

Still it would be better to just sum the amount in your for loop 仍然最好在for循环中加总

var total = 0;

... ...

if (objects == 'amt_1')
   {
     var curAmt = json[i][objects];
     output += '<td class="amt">' + curAmt + '</td>';
     total += parseFloat(curAmt);
   }

... ...

output += '<td>' + total + '</td>';

You can't have more than one element with the same id on a page. 一个页面上不能有多个具有相同ID的元素。 $("#someId") will always select at most 1 element. $("#someId")将始终选择最多1个元素。 So, your first problem is you don't even have a list of elements. 因此,您的第一个问题是您甚至没有元素列表。 You can solve that by using class instead. 您可以改为使用class来解决。

Once you resolve the id issue, the trouble you'll have is you are trying to add text as though it were a number. 解决id问题后,您将遇到的麻烦是,您试图像添加数字一样添加文本。 First, you convert text to a number in one loop, then you loop through your entire collection again and try to add the textual numbers. 首先,您将一个循环中的文本转换为数字,然后再次遍历整个集合并尝试添加文本数字。 Just get the total during the first loop. 只需在第一个循环中获得总数即可。 You'll be adding numbers instead of text, and you'll only have to iterate your collection once. 您将添加数字而不是文本,并且只需要迭代一次您的集合。

Also, you don't need to iterate the keys of an object just get a property. 另外,您无需仅通过获取属性就可以迭代对象的键。 You can just reference the property directly: json[i].amt_1 您可以直接引用该属性: json[i].amt_1

While we're at it, let's not build up the html string, but instead just create the DOM elements directly. 在此过程中,我们不要建立html字符串,而是直接创建DOM元素。 And take advantage of $.each() to do our looping. 并利用$.each()进行循环。

var total = 0;
$.each(json, function (i, item) {
    var row  = $('<tr>').appendTo("#details");
    $("<td>").appendTo(row).addClass("amt").text(item.amt_1);
    total += item.amt_1;
});
var row = $("<tr>").appendTo("#details");
$("<td>").appendTo(row).text(total);

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

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