日期:2014-05-16  浏览次数:20672 次

jquery ajax经常发生重复提交表单的现象?
代码如下:

$(".new").click(function(){
  $("#EditForm").on('show', function(){
    document.getElementById('id_date').value = "";
    document.getElementById('id_content').value = "";
    $(".save").click(function(){
      var date_data = document.getElementById('id_date').value;
      var content_data = document.getElementById('id_content').value;
      var post_data = {'date':date_data, 'content':content_data, 'flag':'new'};
      $.ajax({
        type:'POST',
        url:'{% url diary_views_mydiary uid month %}',
        data:post_data,
        dataType:'json',
        async:false,
        success:function(ret){alert(ret);}
      });
    });
    $(".cancel").click(function(){
      $("#EditForm").modal('hide');
    });
  });
});



views.py:

def MyDiary(request, uid, month):
    if request.is_ajax():
        flag = request.POST.get('flag')
        """New"""
        if flag == 'new':
            new_date = request.POST.get('date')
            new_content = request.POST.get('content')
            new_diary = Diary(user_id=uid, date=new_date, content=new_content)
            try:
                new_diary.save()
                ret = "1"
                return HttpResponse(ret)
            except:
                ret = "0"
                return HttpResponse(ret)



<div class="modal hide fade" id="EditForm">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>新建</h3>
  </div>
  <div class="modal-body">
    <form action="" method="POST">
      <fieldset>
        {% csrf_token %}
        <div class="control-group">