在 Javascript 中实现赞成/反对票系统,但无法正确计算

发布于 2024-10-01 07:24:59 字数 856 浏览 1 评论 0原文

var $arrow       = $(this);
var $sibling     = $arrow.siblings('span.arrow');
var $score       = $arrow.siblings('span.score');

var vote         = $arrow.hasClass('up') ? 'up' : 'down';
var alreadyVoted = $sibling.hasClass('voted');

if (!USER_LOGGED_IN)
{
    alert('You must be logged into vote');
}
else if (!$arrow.hasClass('voted'))
{
    if (alreadyVoted)
        $sibling.removeClass('voted');

    $arrow.addClass('voted');
    $score[0].innerHTML = parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1);
}

我有一个赞成票和反对票按钮。这些按钮旁边显示“当前分数”,我希望在投票时增加/减少这些按钮。

例如,如果他们加载页面并看到分数为 200。当他们投票时,分数将更改为 201。当他们投反对票时,分数需要更改为 199。为什么?因为如果他们在投赞成票后投反对票(改变主意),那么投票需要从原始分数开始。不是他们通过投票创造的新分数。

基本上,如果他们先投赞成票,然后投反对票,那么当前的分数会回到原始分数。他们的投票没有投票。

我在完成这项工作时遇到了困难,所以他们投票了......

var $arrow       = $(this);
var $sibling     = $arrow.siblings('span.arrow');
var $score       = $arrow.siblings('span.score');

var vote         = $arrow.hasClass('up') ? 'up' : 'down';
var alreadyVoted = $sibling.hasClass('voted');

if (!USER_LOGGED_IN)
{
    alert('You must be logged into vote');
}
else if (!$arrow.hasClass('voted'))
{
    if (alreadyVoted)
        $sibling.removeClass('voted');

    $arrow.addClass('voted');
    $score[0].innerHTML = parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1);
}

I have an upvote and downvote button. A 'current score' is shown next to these buttons which I would like to increment/decrement when a vote is cast.

For example, if they load the page and see the score is 200. When they upvote, the score will change to 201. When they downvote, the score needs to change to 199. Why? Because if they downvote after upvotting (change their mind) then the vote needs to go from the original score. Not the new score they created by upvotting.

Basically, if they upvote and then downvote, the score, currently, goes back to the original score. Their vote isn't cast.

I'm having trouble making this work so their vote is cast...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2024-10-08 07:24:59

将这一点: 更改

parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1);

为:

parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1) + ((alreadyVoted) ? ((vote == 'up') ? 1 : -1) : 0);

它很冗长,但它会在紧要关头起作用。

change this bit:

parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1);

to this:

parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1) + ((alreadyVoted) ? ((vote == 'up') ? 1 : -1) : 0);

Its verbose, but it will work in a pinch.

昔梦 2024-10-08 07:24:59

据我所知,这正在按预期进行。如果你投赞成票,那就+1。如果您随后投反对票(递减),则结果为负一,n + 1 - 1 的结果应为 n。如果他们想实现真正的否决,他们应该再次否决。

As far as I can tell, this is working as expected. If you upvote, that's +1. If you then downvote (decrement) it's minus one, and the result of n + 1 - 1 should be n. They should have to downvote again if they want to achieve a true downvote.

软的没边 2024-10-08 07:24:59

听起来如果用户已经投票了(所以 vote = 1),然后他们单击向下箭头,那么您希望 vote = -1 而不是 vote = 0(就像 StackOverflow 所做的那样)。如果是这样,您需要在脚本中明确检测该条件,例如:

var voteValue = parseInt($score[0].innerHTML, 10);
if (alreadyVoted) {
    $sibling.removeClass('voted');
    // Undo the previous vote
    voteValue += $sibling.hasClass('up') ? -1 : 1;
}

// Apply the new vote
$arrow.addClass('voted');
voteValue += (vote == 'up') ? 1 : -1;
$score[0].innerHTML = voteValue;

或者,当然,如果它确实是“向上”或“向下”(就像这样),它会得到很多更简单:

if (alreadyVoted)
    $sibling.removeClass('voted');

$arrow.addClass('voted');
$score[0].innerHTML = vote == 'up' ? 1 : -1;

...因为实际上根本不需要添加/减去。

It sounds like if the user has already voted up (so vote = 1) and then they click the down arrow instead, you want vote = -1 instead of vote = 0 (much as StackOverflow does it). If so, you'll need to explicitly detect that condition in your script, something like:

var voteValue = parseInt($score[0].innerHTML, 10);
if (alreadyVoted) {
    $sibling.removeClass('voted');
    // Undo the previous vote
    voteValue += $sibling.hasClass('up') ? -1 : 1;
}

// Apply the new vote
$arrow.addClass('voted');
voteValue += (vote == 'up') ? 1 : -1;
$score[0].innerHTML = voteValue;

Or, of course, if it really is either "up" or "down" (like SO), it gets a lot simpler:

if (alreadyVoted)
    $sibling.removeClass('voted');

$arrow.addClass('voted');
$score[0].innerHTML = vote == 'up' ? 1 : -1;

...since there's really no need for adding/subtracting at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文