1
<script type="text/javascript">
function vote(element, val){
prev_vote=0;
vote = 100
can_vote = element.find('.vote-permission').attr('value')
alert(can_vote);
if (can_vote=='yes'){
if (prev_vote==0){
alert("previous vote is 0");
// user has not cast vote for image before
vote_score = vote + val;
element.find('.vote-count-post').text(vote_score);
}
else if (prev_vote != 0){
// user has cast a vote before
if (prev_vote == 1 && val == -1){
vote_score = vote - 2;
element.find('.vote-count-post').text(vote_score);
}
else if (prev_vote == -1 && val == 1){
vote_score = vote + 2;
element.find('.vote-count-post').text(vote_score);
}
}
}
};
$(document).on('click', '.votedown', function(e){
alert($(e.target).prev().text());
vote($(e.target).parent(), -1);
alert('vote down is done');
});
$(document).on('click', '.voteup', function(e){
$(e.target).css('background-position', '0 -222px');
vote($(e.target).parent(), 1);
alert('vote up is done');
});
</script>
上述javascript函數是我正在實施的向上和向下投票系統的腳手架。當第一次對upvote或downvote按鈕進行點擊時,函數按照預期的方式使用vote()調用,但在第二次點擊時,vote()函數被說成沒有被定義。任何想法爲什麼?javascript函數未在第二次調用時定義
謝謝你。這幾乎解決了它。但只是說。範圍規則相當不友善。 – cobie