我正在构建一个Q &一个可以评论问题及其答案的网站。它是一个带有ajax的线程评论系统。django:使用jquery动态插入评论表单
这是JavaScript部分:
function bindPostCommentHandler()
{
$('.commentFormWrapper form').submit(function() {
var current = $(this);
$.ajax({
type: "POST",
data: current.serialize(),
url: "{% comment_form_target %}",
cache: false,
dataType: "html",
beforeSend:function(xhr){
$('.submit', current).html('<img id="preloader" class="va-mid" src="{{MEDIA_URL}}img/indicator.gif" title="processing.." />');
$('#commentError').remove();
},
success: function(html, textStatus) {
current.parent().replaceWith(html);
bindPostCommentHandler();
},
error: function(xhr, textStatus, errorThrown) {
$('#commentError').remove();
$('.submit', current).html('<input type="submit" name="submit" class="submit-post small-button" value="Submit" />');
if(xhr.status == 400){
current.before('<li id="commentError" class="no-bullet errornote margin10">OOPS ! your comment looked liked a spam. Try again with some modifications.</li>');
}else {
current.before('<li id="commentError" class="no-bullet errornote margin10">Your comment was unable to be posted at this time. You may try after sometime.</li>');
}
//bindPostCommentHandler();
}
});
return false;
});
}
$(document).ready(function() {
bindPostCommentHandler();
});
HTML部分:
<!-- comment form for question -->
<div class="commentFormWrapper">
{% render_comment_form for question %}
</div>
<!-- comment form for answers -->
{% for answer in question.answers.all %}
<div class="commentFormWrapper">
{% render_comment_form for answer %}
</div>
问题是,当只有在它的工作原理页面的单一形式顺利。有多种形式的工作,但多次发送请求到服务器(以倍数增长)。
此外,它会更好地动态插入/删除窗体。但是,如果我手动添加表单的html,我会错过注释表单中的csrf标记和时间戳字段。任何人都有解决方案?
你什么时候打电话给你bindPostCommentHandler功能?它可能被称为多次? – codeape 2010-01-18 13:00:48
我在页面加载时调用一次,并在评论发布成功时调用一次。你可以在$ .ajax({... success:function(..){--here--}中看到这个问题。 问题是由于html标签类名引起的,我应该使用id,然后绑定。 – anand 2010-01-18 15:26:05
style =“tdisplay:none;”,应该是style =“display:none;”? – dotty 2010-01-18 16:56:55