我需要在用户输入上附加点击事件的新值,我正在使用跟随标记和代码来执行此操作。但是由于使用.each()
,表一次又一次地加载重复的值。能告诉我如何停止此操作并添加(追加)新插入的值
<table>
<thead>
<tr>...</tr>
</thead>
<tbody id="compValues">
</tbody>
</table>
var compareData = [];
var data = {
compItem1 : uName,
compItem2 : uLName,
compItem3 : uIimage,
compItem4 : memberType,
compItem5 : planTarget,
compItem6 : selectedPlan,
compItem7 : selectedSTime,
compItem8 : historicDis,
compItem9 : projectsNumber,
compItem10: projectsCost,
compItem11: projectsRoads,
compItem12: projectsPhase
};
compareData.push(data);
$.each(compareData, function (index, item) {
var eachrow = "<tr>"
+ "<td>" + item["uName"] + "</td>"
+ "<td>" + item["uLName"] + "</td>"
+ "<td>" + item["uIimage"] + "</td>"
+ "<td>" + item["planTarget"] + "</td>"
+ "<td>" + item["selectedPlan"] + "</td>"
+ "<td>" + item["selectedSTime"] + "</td>"
+ "<td>" + item["historicDis"] + "</td>"
+ "<td>" + item["projectsNumber"] + "</td>"
+ "<td>" + item["projectsCost"] + "</td>"
+ "<td>" + item["projectsRoads"] + "</td>"
+ "<td>" + item["projectsPhase"] + "</td>"
+ "</tr>";
$('#compValues').append(eachrow);
});
由于
答案 0 :(得分:0)
如果该项目有唯一的密钥或ID,您可以通过将其添加为tr的ID来检查它是否已经在表格中的任何位置,只需检查代码是否为已经存在类似的东西:
//if the unique key or id is uName, then do something like this
if ($('tr#' + item["uName"]).length === 0) {
var eachrow = "<tr id='"+ item[uName] +"'>"
+ "<td>" + item["uName"] + "</td>"
+ "<td>" + item["uLName"] + "</td>"
+ "<td>" + item["uIimage"] + "</td>"
+ "<td>" + item["planTarget"] + "</td>"
+ "<td>" + item["selectedPlan"] + "</td>"
+ "<td>" + item["selectedSTime"] + "</td>"
+ "<td>" + item["historicDis"] + "</td>"
+ "<td>" + item["projectsNumber"] + "</td>"
+ "<td>" + item["projectsCost"] + "</td>"
+ "<td>" + item["projectsRoads"] + "</td>"
+ "<td>" + item["projectsPhase"] + "</td>"
+ "</tr>";
$('#compValues').append(eachrow);
}