【发布时间】:2017-09-15 18:30:50
【问题描述】:
我正在尝试做一个简单的投票按钮,但每次重新加载每个帖子只能工作一次。
它应该如何工作: 单击“+”-> 更新查询(在 django 中)-> 重新加载 div(将“+”更改为“-”)-> 允许单击“-”以撤消投票
它在我第一次投票后显示“-”按钮,但当我点击它时,没有任何反应。只有当我点击“F5”并重新加载页面时它才有效。
控制台没有错误。
... some django scripts to check which button should be displayed ...
<span class="upvote"><span postId="{{post.id}}" class="vote" href="upvote/{{post.id}}">+</span></span>
<span class="downvote"><span postId="{{post.id}}" class="vote" href="downvote/{{post.id}}">-</span></span>
...
$(document).ready(function(){
$(".vote").click(function(e){
href = $(this).attr("href");
postId = $(this).attr("postId");
$.ajax({
url: href,
success: function() {
$("#post-"+postId+" >header").load(location.href+ " #post-"+postId+">header>*");
},
});
});
正确填写“postId”和“href”属性
【问题讨论】:
-
为什么不在
周围使用 标签,并带有 href 到页面? -
尝试将
$(".vote").click()句柄事件放在$(document).ready()上下文之外。 -
还是一样,没什么变化
标签: javascript jquery ajax