我有一个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
}
]
}
所以我对评论看起来很像系统的upvote / downvote。 这就是它看起来是暂时的:http://imgur.com/AlJBXSP。
所以我实际想要实现的是每当我点击绿色或红色圆圈时,我想访问评论的commentId并使用/发送到服务器。 像这样:
var data = {commentId: idOfComment, typeOfUser: typeOfUser, approvalType: 1 };
dataservice.setApprovalOnEventComments(document.cookie.substring(12), data, function () {
});
我这样做了:var idOfComment = res.comments[i].commentId;
但是这只给了我评论的第一条评论,因为我有一个for循环,它给了我所有的评论。
for (var i = 0; i < res.comments.length; i++) {
var text = res.comments[i].comment;
}
答案 0 :(得分:2)
我会将评论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>' +