每当单击“类似于btn”时,我想将每个记录的注释计数器增加一个。
@foreach (var item in Model.PostComments)
{
<a id=" @item.Id" href="#" class="btn-sm btn-default btn-like"><span class="glyphicon glyphicon-thumbs-up"></span></a>
<span id="commentcounter_@item.Id">@Model.CommentMetrics.Where(a => a.PostCommentId == item.Id).Sum(a => a.VoteValue)</span>
}
虽然jQuery代码是
<script>
$(document).ready(function () {
$('a.btn-like').click(function (e) {
e.preventDefault();
$(this).toggleClass("btn-success");
$(e.target.id).text(parseInt($(e.target.id).text()) + 1);
$(this).prop("disabled", true);
$('#commentcounter').text(function (i, oldVal) {
return parseInt(oldVal, 10) + 1;
})
$.ajax({
url: '@Url.Action("CommentUp", "Posts")',
data: { id: this.id }
});
});
});
</script>
答案 0 :(得分:0)
您可以这样更改您的代码:
@foreach (var item in Model.PostComments)
{
<a id=" @item.Id" href="#" class="btn-sm btn-default btn-like"><span class="glyphicon glyphicon-thumbs-up"></span></a>
<span id="commentcounter_@item.Id" class="commentCountClass">@Model.CommentMetrics.Where(a => a.PostCommentId == item.Id).Sum(a => a.VoteValue)</span>
}
这是单击btn时的javascript代码,因此每个记录的注释计数将增加。而且我认为每个跨度都包含数值。
<script>
$(document).ready(function () {
$('a.btn-like').click(function (e) {
e.preventDefault();
//Here what this code will do. If you will click on any "btn-like". then this will increment the commentcounter for each record by one.
// Here code start for increment counter
$('.commentCountClass').each(function(){
var currentVal = parseint($(this).text());
var updatedVal = currentVal + 1;
$(this).text(updatedVal );
});
});
});
</script>
我删除了剩下的代码,因为我不知道您正在尝试使用该代码。您可以使用我的代码调整代码。谢谢。