django form结合ajax提交数据
在提交评论的时候不想刷新页面,这个时候就需要用到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>
看上去很简单,不过有大作用。
www.notewo.com

浙公网安备 33010602011771号