我有一個投票按鈕,我創建它包含在.vote_div中。 2部分:.vote_num爲投票總數,.vote爲投票按鈕。該頁面有一個項目列表,所以我需要確保當用戶點擊.vote,它會改變相應的.vote_num + 1.JS:點擊一個div,更改另一個div值
我的JS函數工作時.vote實際上是總票數,但現在我我分開了兩個。如何抓取.vote點擊右側的.vote_num?
謝謝!
<script>
$(".vote").click(function() {
var votes = $(this).attr('votes');
$.post(
'{% url vote %}', {
"id":this.id,
}, function(data) {});
this.className = "voted";
$(this).text(parseInt(votes) + 1);
return false;
});
</script>
<div class="vote_div">
<span class="vote_num" votes='{{host.num_votes}}'>{{host.num_votes}}</span>
<span class="vote" id="{{host.user.id}}">Vote</span>
</div>
編輯& SOLUTION:
得到它的工作使用$(本).parent():
<script>
$(".vote").click(function() {
var votes = $(this).parent().find('.vote_num').attr('votes');
$.post(
'{% url vote %}', {
"id":this.id,
}, function(data) {});
this.className = "voted";
votes = parseInt(votes) + 1;
$(this).parent().find('.vote_num').text(votes);
return false;
});
</script>
感謝Davy8,得到了此解決方案以上的解決方案 – Emile
@Emile添加了鏈接到文檔以供進一步閱讀。 – Davy8