[英]How to stop innerHtml repeating itself over and over again in a table?
I have made an array with objects that get there info from three different user variables, however on one of these there are many sub variables that i don't want it to repeat itself every time the user presses the select button(which updates the table) instead i want it to just add onto (or take away) from the sections that it already has in the table. 我用对象组成了一个数组,这些对象从三个不同的用户变量那里获取信息,但是在其中一个上有很多子变量,我不希望它每次用户按下选择按钮时都重复一次(更新表) ),我希望它只是添加到表中已有的部分(或从中删除)。 thanks(if you need the variable code let me know) I have been trying to solve thi for a while now!
谢谢(如果您需要可变代码,请告诉我)我已经尝试解决了一段时间了! please help!!
请帮忙!!
//creating array
var gProducts = new Array();
var gTotalCost = 0;
// Adding Products to array gProducts
function addProduct
{
var product = new Object();
product.name = name;
product.cost = cost;
gProducts.push(product);
gTotalCost += parseInt(cost)
}
//Getting products from array, use of for in loop setting new table rows in blank var for each array item
function renderProducts()
{
var HTMLadd = ""
for (var i in gProducts)
{
if( gProducts[i].cost > 0){
HTMLadd = HTMLadd +
"<tr>"+
"<td class='tableSettings00' id=tableRow2 >" + gProducts[i].name +
"</td>"+
"<td class='tableSettings'>€<span id=tableRow2part2>" + gProducts[i].cost +
"</span></td>"+
"</tr>";
}
else
{
}
}
document.getElementById('tableRow').innerHTML = HTMLadd;
}
You're using a for in loop, when you probably wanted just a for loop. 当您可能只需要for循环时,您正在使用for in循环。
Change this line to 将此行更改为
for (var i in gProducts)
to 至
for (var i = 0; i < gProducts.length; i++)
With a for/in loop, the variable i
will be an object, and not an integer. 在for / in循环中,变量
i
将是一个对象,而不是整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.