我有一个 JSON 响应:
{
"errorMessage":0,
"comments":[
{
"commentId":37,
"name":"name",
"comment":"zxcasdqwerty",
"timestamp":1460027788,
"typeOfUser":1,
"appropriate":0,
"inappropriate":0,
"allreadySetApproval":0
},
{
"commentId":36,
"name":"name",
"comment":"123",
"timestamp":1460027777,
"typeOfUser":1,
"appropriate":0,
"inappropriate":0,
"allreadySetApproval":0
}
]
}
所以我对相似评论系统有赞成/反对票。 这是临时的:/image/rdBSx.jpg .
所以我实际上想要实现的是,每当我单击绿色或红色圆圈时,我想访问评论的 commentId 并使用/将其发送到服务器。 像这样的事情:
var data = {commentId: idOfComment, typeOfUser: typeOfUser, approvalType: 1 };
dataservice.setApprovalOnEventComments(document.cookie.substring(12), data, function () {
});
我这样做了:var idOfComment = res.comments[i].commentId;
但这只给了我评论中的第一个commentId,因为我有一个for循环给了我所有评论。
for (var i = 0; i < res.comments.length; i++) {
var text = res.comments[i].comment;
}
最佳答案
我会将评论 ID 传递给 onclick 函数。
likeComment = function ( commentid ) {
var data = {commentId: commentid, typeOfUser: typeOfUser, approvalType: 1 };
dataservice.setApprovalOnEventComments(document.cookie.substring(12), data, function () {
});
};
并将其添加到您的 html 中。
'<a href="#"><img onclick="likeComment('+ idOfComment +')" title="like" class="img-responsive img-circle" src="../../images/my_icon_test_green.png"/></a>' +
关于javascript - 如何使用 JSON 的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478580/