我是JavaScript和jQuery的新手。这是我的JSON文件。我想为jQuery.each()添加一个jQuery onclick事件。这是我的代码。添加onclick事件到jQuery.each()
{
"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);
});
但我想添加点击功能与我的每一个给予的价值。 这是我的代码在下面,但我没有定义。
$.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返回一个点击的值。
你想提取什么? –
请花些时间阅读帮助页面,尤其是名为[“我可以询问什么主题?”(http://stackoverflow.com/help/on-topic)和[“应该提供哪些类型的问题我避免问?“](http://stackoverflow.com/help/dont-ask)。更重要的是,请阅读[Stack Overflow问题清单](http://meta.stackexchange.com/q/156810/204922)。您可能还想了解[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
RequestId,CustomerId和RequestCharge – 1plus