简体   繁体   English

JavaScript星级评分系统

[英]JavaScript Star Rating System

I have a working star rating system, but If a user has already voted and changes their mind on the vote, they cannot reset their vote. 我有一个工作星级评定系统,但是如果用户已经投票并改变了投票决​​定,他们将无法重置投票。

Here is what I have in the Javascript: 这是我在Javascript中拥有的内容:

<script type="text/javascript">

var set = false;
var v = 0;
var a;

function loadStars() {
    star1 = new Image();
    star1.src = "images/star1.png";
    star2 = new Image();
    star2.src = "images/star2.png";
}

function highlight(x) {
    if (set == false) {
        y = x * 1 + 1
        switch (x) {
        case "1":
            document.getElementById(x).src = star2.src;
            document.getElementById('vote').innerHTML = "One star";
            break;
        case "2":
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            document.getElementById('vote').innerHTML = "Two stars"
            break;
        case "3":
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            document.getElementById('vote').innerHTML = "Three stars"
            break;
        case "4":
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            document.getElementById('vote').innerHTML = "Four stars"
            break;
        case "5":
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            document.getElementById('vote').innerHTML = "Five stars"
            break;
        }
    }
}

function losehighlight(x) {
    if (set == false) {
        for (i = 1; i < 6; i++) {
            document.getElementById(i).src = star1.src;
            document.getElementById('vote').innerHTML = ""
        }
    }
}

function setStar(x) {
    y = x * 1 + 1
    if (set == false) {
        switch (x) {
        case "1":
            a = "1"
            flash(a);
            break;
        case "2":
            a = "2"
            flash(a);
            break;
        case "3":
            a = "3"
            flash(a);
            break;
        case "4":
            a = "4"
            flash(a);
            break;
        case "5":
            a = "5"
            flash(a);
            break;
        }
        set = true;
        insertrating(x, '<?=$themeid?>');
        document.getElementById('vote').innerHTML = "Thank you!";
    }

    function flash() {
        y = a * 1 + 1
        switch (v) {
        case 0:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star1.src;
            }
            v = 1
            setTimeout(flash, 200)
            break;
        case 1:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            v = 2
            setTimeout(flash, 200)
            break;
        case 2:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star1.src;
            }
            v = 3
            setTimeout(flash, 200)
            break;
        case 3:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            v = 4
            setTimeout(flash, 200)
            break;
        case 4:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star1.src;
            }
            v = 5
            setTimeout(flash, 200)
            break;
        case 5:
            for (i = 1; i < y; i++) {
                document.getElementById(i).src = star2.src;
            }
            v = 6
            setTimeout(flash, 200)
            break;
        }
    }

</script>

How would I go about allowing the user to resubmit a vote? 我将如何允许用户重新提交投票?

My html looks like this: 我的html看起来像这样:

<span style="float: left; text-align: left;"> Rate This Theme<br />
    <img src="images//star1.png" onMouseOver="highlight(this.id)" onClick="setStar(this.id)" onMouseOut="losehighlight(this.id)" id="1" style="float: left;" /> 
    <img src="images/star1.png" onMouseOver="highlight(this.id)" onClick="setStar(this.id)" onMouseOut="losehighlight(this.id)" id="2" style="float: left;" /> 
    <img src="images/star1.png" onMouseOver="highlight(this.id)" onClick="setStar(this.id)" onMouseOut="losehighlight(this.id)" id="3" style="float: left;" /> 
    <img src="images/star1.png" onMouseOver="highlight(this.id)" onClick="setStar(this.id)" onMouseOut="losehighlight(this.id)" id="4" style="float: left;" /> 
    <img src="images/star1.png" onMouseOver="highlight(this.id)" onClick="setStar(this.id)" onMouseOut="losehighlight(this.id)" id="5" style="float: left;" /> 
</span> 

bootstrap V4 font-awesome pro jquery bootstrap V4字体真棒专业jQuery

$('.starts i').click(function(){
$('.starts i').removeClass('fas').addClass('fal');
var sindex=$('.starts i').index(this);
    for(var i=0;i<=sindex;i++){
        $('.starts i').eq(i).removeClass('fal').addClass('fas');
    }
})

<div class="starts h4 mt-3" style="color:#FBC02D">
<span class=" ml-2"><i class="fal fa-star"></i></span>
<span class="checked ml-2 title"><i class="fal fa-star"></i></span>
<span class="checked ml-2"><i class="fal fa-star"></i></span>
<span class="checked ml-2"><i class="fal fa-star"></i></span>
<span class="checked ml-2"><i class="fal fa-star"></i></span>
</div>

As far as I can tell, the only thing preventing the user from re-voting is your set variable. 据我所知,阻止用户重新投票的唯一方法是set变量。

   if (set==false)
   {
   ...  
   set=true;

Just get rid of the if clause and it should run even if set==true 只要摆脱if子句,即使set==true ,它也应该运行

Per Tomalak's comment, i forgot to mention that: 根据Tomalak的评论,我忘了提到:

If the backend doesn't support this, then it will insert a new vote rather than changing the existing one. 如果后端不支持此功能,那么它将插入一个新的投票,而不是更改现有的投票。 And since there doesn't appear to be any user identification, that would appear to be the case 而且由于似乎没有任何用户标识,因此情况似乎如此

If you don't have any user authentication, at least use a cookie or something to keep track of which user is what so that the vote is edited, not a new one created. 如果您没有任何用户身份验证,请至少使用Cookie或其他方法来跟踪哪个用户是哪个用户,以便编辑投票,而不是创建新的投票。

As for setting cookies, I'd recommend setting a cookie via Javascript once the user votes the first time. 至于设置cookie,我建议一旦用户第一次投票就通过Javascript设置cookie。 On the backend, whatever you're using for server-side technology should check to see if a cookie already exists, and if it does, edit the ranking entry instead of creating a new one. 在后端,无论用于服务器端技术的是什么,都应检查cookie是否已存在,如果存在,则编辑排名条目而不是创建新的cookie。 Here's a good resource on cookies in JS: http://www.quirksmode.org/js/cookies.html 这是有关JS中Cookie的好资源: http : //www.quirksmode.org/js/cookies.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM