在提交评论的时候不想刷新页面,这个时候就需要用到ajax来提交数据 。我使用的方法如下:
<script type="text/javascript">
$(document).ready(function(){
$("#post_answer_form").submit(function(){
var data = $(this).serialize();
$.ajax({
type: $(this).attr('method'),
url: "{% url article.views.answer article.id %}",
data:data,
beforeSend: function(XMLHttpRequest){
},
success: function(response){
if(response == 'success'){
var content = $('#id_content');
$('#answer-list').append('<p>'+content.val()+'</p>');
content.val('');
}
},
complete: function(XMLHttpRequest, textStatus){
},
error: function(response){
$('#answer-list').html(response);
}
});
return false;
});
});
</script>