我正在做一个项目,我的视图中有两个锚点(用于投票功能),
我有一个 div,里面有一个<ul>
,在 3 中,我有一个分别<li>
用于 upvote、vote count(in <h2>)
和 anchor downvote的锚
我想要功能,当我点击任何锚点时,h2 html 会显示投票计数,我已经实现了该功能,但是因为
<div class="voting" style="margin-left:20px;">
<ul>
<li class="addvote"><a href="#" class="voteAnswer" answerid="@answer.AnswerID" name="Voted">
Up</a></li>
<li class="votecounter">
<h2>
@answer.AnswerLikes.Where(a => a.IsActive == true).Count()</h2>
</li>
<li class="subvote"><a href="#" class="voteAnswer" answerid="@answer.AnswerID" name="Voted">
Down</a></li>
</ul>
</div>
这是我的 JS
$(".voteAnswer").click(function (event) {
var answerid = $(this).attr('answerid');
var name = $(this).attr('name');
var id = $(this).attr('id');
var output = $(this);
$.ajax({
url: ResourceAjaxUrl.VoteUnvoteTheAnswer,
type: "POST",
data: JSON.stringify({ answerID: answerid }),
dataType: "html",
contentType: "application/json; charset-utf-8",
success: function (Result) {
alert("Voted");
// $(output).html("Voted (" + Result + ")");
$(output).closest("li").find("h2").html(Result);
$(output).attr("name", "Voted");
},
error: function (msg) {
alert("Unable to Vote answer: " + msg);
}
});
event.preventDefault();
});
我已经尝试使用 $(output).closest("li").find(".votecounter")
但它仍然无法正常工作