- 需求:如下页面,一条问题记录对应多条回答记录,用户不登录无法顶或踩,且当前登录用户只能对某一个问题,或某一个回答顶或踩一次,当然同一问题下的不同回答,也各只能顶或踩一次,最好加上时间限制,规定多久后又可以点赞。
- 情况:最初我用的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){
$("#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){
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();
var currentId = p3;
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();
var currentId = p3;
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>