【发布时间】:2017-09-27 09:44:51
【问题描述】:
我是 Javascript 和 jQuery 的新手。这是我的 JSON 文件。我想向 jQuery.each() 添加一个 jQuery onclick 事件。这是我的代码。
{
"1": [{
"RequestId": "1",
"CustomerId": "1",
"RequestCharge": "100.00",
"Trade": ""
}],
"2": [{
"RequestId": "1",
"CustomerId": "1",
"RequestCharge": "100.00",
"Trade": ""
}],
"3": [{
"RequestId": "1",
"CustomerId": "1",
"RequestCharge": "50.00",
"Trade": ""
}]
}
下面的代码运行良好
$.each(output, function(i) {
var tr = $('<tr/>');
tr.append("<td>" + output[i][0].RequestId + "-" +"</td>");
tr.append("<td>" + output[i][0].CustomerId + "-" + "</td>");
tr.append("<td>" + output[i][0].RequestCharge + "</td>");
$("#whad").append(tr);
});
但我想添加一个点击功能,每个给定的 i 值。 这是我下面的代码,但我未定义。
$.each(output, function(i) {
var tr = $('<tr/>');
tr.append("<td>" + output[i][0].RequestId + "-" +"</td>");
tr.append("<td>" + output[i][0].CustomerId + "-" + "</td>");
tr.append("<td>" + output[i][0].RequestCharge + "</td>");
$("#whad").append(tr).click(function() {
console.log("This is the value of i clicked: " + this.i)
});
});
这是我的html。
<table id = "whad"> </table>
我需要 this.i 来返回点击的值。
【问题讨论】:
-
你要提取什么?
-
请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples。
-
RequestId、CustomerId 和 RequestCharge
-
@RoryMcCrossan 我相信我已经将我的问题编辑得足够清楚了。
标签: javascript jquery json