代码总结:顶或踩功能的单次性和限时性

  • 需求:如下页面,一条问题记录对应多条回答记录,用户不登录无法顶或踩,且当前登录用户只能对某一个问题,或某一个回答顶或踩一次,当然同一问题下的不同回答,也各只能顶或踩一次,最好加上时间限制,规定多久后又可以点赞。
  • 情况:最初我用的cookie,便于操作,这肯定不合适,但一时没想到别的方式。总体的思路是:对问题顶或踩后拼接用户id和问题id;对问题的某条回答顶或踩后拼接用户id和回答id,每次顶或踩操作前验证其是否包含
<!--问题顶或踩 -->
<div class="vote_collect_wrapper">
    <div class="vote_collect">
    <a class="vote_up" title="顶" id="qup" count="0"> </a>
    <span id="questionVoteCount" class="vote_count" data="${loginer.id}" qdata="${qInfo.qid}">${qInfo.votes}</span>
    <a class="vote_down" title="踩" id="qdn" count="0"> </a>
    </div>
    <script type="text/javascript">
    $("#qup").click(function(){
        var currentId = $("#questionVoteCount").attr("data");
        if(currentId == ""){
            alert("请先登录");
            return;
        }
        var qid = $("#questionVoteCount").attr("qdata");
        var currentVotes = $("#questionVoteCount").html();
        var status = $.cookie("voteQuestion");
        if(status == undefined || status.indexOf(currentId+qid) == -1){
        //cookie里voteUserId为空,或者voteUserId里不包含当前用户id,就可以投票
                            $("#questionVoteCount").html(parseInt(currentVotes)+1);
    $.ajax({
    url:"vote.do",
    data: {"change":"up","qid":qid,"votes":currentVotes},
    type: "post",
    });                         $.cookie("voteQuestion",status+","+currentId+qid,{expires:30});
}else{
    alert("大侠,你已投过票了\n小提示:同一个用户ID在30天内不能重复此操作");
}
});
$("#qdn").click(function(){
        var currentId = $("#questionVoteCount").attr("data");
        if(currentId == ""){
            alert("请先登录");
            return;
        }
        var qid = $("#questionVoteCount").attr("qdata");
        var currentVotes = $("#questionVoteCount").html();
        var status = $.cookie("voteQuestion");
        if(status == undefined || status.indexOf(currentId+qid) == -1){
        //cookie里voteUserId为空,或者voteUserId里不包含当前用户id,就可以投票
        vote = parseInt(currentVotes)-1;
            if(vote > 0){
                $("#questionVoteCount").html(vote);
            }else{
                $("#questionVoteCount").html(0);
            }
            $.ajax({
                    url:"vote.do",
                    data: {"change":"dn","qid":qid,"votes":currentVotes},
                    type: "post",
            });                     $.cookie("voteQuestion",status+","+currentId+qid,{expires:30});
}else{
        alert("大侠,你已投过票了\n小提示:同一个用户ID在30天内不能重复此操作");
}
});
</script>
</div>
<article class="answer_detail" data="${ar.supports}" time="${ar.rspTime}">
                    <div class="vote_collect_wrapper">
                        <div class="vote_collect">
                            <a class="vote_up" title="顶" onclick="up(${vs.index},${ar.aid},${loginer.id})"> </a>
                            <span class="vote_count" id="count${vs.index}">${ar.supports}</span>
                            <a class="vote_down" title="踩" onclick="down(${vs.index},${ar.aid},${loginer.id})"> </a>
                        </div>
                    </div>
                    <div class="answer-content-detail">
                        <section>
                        <p>
                            ${ar.content}
                        </p>
                        </section>
                    </div>
                    <div class="userdetail">
                        <div class="userinfo">
                            <span class="uname">${ar.nickName}</span>
                            <span class="edittime">${ar.rspTime}</span>
                        </div>
                        <div>
                            <img src="img/photo/${ar.photo}" />
                        </div>
                    </div>
                    </article>
                    </c:forEach>
                    <!-- 在这给所有行绑定事件实现投票功能 -->
                    <script>
                        function up(p1,p2,p3){
                            //本来只用传下面三个参数,修改数据库
                            var id = "#count"+p1;
                            var aid = p2;
                            var currentVotes = $(id).html();
                            //我通过cookie的方式防止同一个用户,通过刷新频繁操作投票
                            var currentId = p3;//当前用户id
                            if(p3 == undefined){
                                alert("请先登录");
                                return;
                            }
                            var status = $.cookie("voteAnswer");
                            if(status == undefined || status.indexOf(currentId+""+aid) == -1){
                                $(id).html(parseInt(currentVotes)+1);
                                $.ajax({
                                    url:"voteans.do",
                                    data:{"change":"up","aid":aid,"votes":currentVotes},
                                    type:"post",
                                    success:function(){

                                    }
                                });
                                $.cookie("voteAnswer",status+","+currentId+aid,{expires:30});
                            }else{
                                alert("大侠,你已投过票了\n小提示:同一个用户ID在30天内不能重复此操作");
                            }
                        }
                        function down(p1,p2,p3){
                            //本来只用传下面三个参数,修改数据库
                            var id = "#count"+p1;
                            var aid = p2;
                            var currentVotes = $(id).html();
                            //我通过cookie的方式防止同一个用户,通过刷新频繁操作投票
                            var currentId = p3;//当前用户id
                            if(p3 == undefined){
                                alert("请先登录");
                                return;
                            }
                            var status = $.cookie("voteAnswer");
                            if(status == undefined || status.indexOf(currentId+""+aid) == -1){
                                vote = parseInt(currentVotes)-1;
                                if(vote > 0){
                                    $(id).html(vote);
                                }else{
                                    $(id).html(0);
                                }
                                $.ajax({
                                    url:"voteans.do",
                                    data:{"change":"dn","aid":aid,"votes":currentVotes},
                                    type:"post",
                                    success:function(){

                                    }
                                });
                                $.cookie("voteAnswer",status+","+currentId+aid,{expires:30});
                            }else{
                                alert("大侠,你已投过票了\n小提示:同一个用户ID在30天内不能重复此操作");
                            }
                        }

                    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值