【发布时间】:2021-02-01 07:05:14
【问题描述】:
我正在尝试在 for 循环内的 onclick 函数中传递对象数组。数组的范围仅在 for 循环内,所以我不能像 this 那样做。 我的代码示例如下:
for (var j = 0; j < globalVariables.countryColl.length; j++) {
var testArray = $.map(empCatgry2Arr, function (e, i) {
return {
Id: e.ID,
company: e.Company,
dept: e.Department
}
});
html += "<tr>" +
"<td>" + globalVariables.countryColl[j].Title + "</td>" +
'<td class="fc-green" onclick="ShowEmployeeDetails(' + testArray + ')">' + empCatgry2Count + '</td>' +
"</tr>";
}
html += "</tbody></table>";
$("#tablediv").append(html);
但是当我检查页面时,HTML 看起来像:"<td class="fc-green"><a onclick="showEmployeeDetails([object Object])">1</td>"
最初我试图传递整个 empCatgry2Arr 数组,我只使用 map 函数来使用所需的键值。
【问题讨论】:
-
如果你能在 jsFiddle 上添加你的问题就太好了
-
您希望在 HTML 中呈现什么?您正在将字符串与数组连接起来。您是否期待逗号分隔的列表或其他内容?
-
这回答了您的问题吗? stackoverflow.com/a/65989148/5236174
标签: javascript jquery arrays datatables