2015-02-05 155 views
0

我找不到錯誤,但它提交了兩次表單。請幫我看看我在這裏錯過了什麼。Ajax表單提交兩次?

$('#supplierForm').on('submit', function(e) { 
       e.preventDefault(); 
       if ($(this).valid()) { 
        $.ajax({ 
         async: false, 
         data: $("#supplierForm").serialize(), 
         url: '{{ url('supplier_edit_new') }}', 
         type: 'POST', 
         success: function (data) { 
           window.location.reload(); 

         } 
        }); 
       } 
       return false; 
      }) 

; 

爲了驗證腳本

$("document").ready(function(){ 

    $('#supplierForm').validate({ 
     errorClass: 'help-block', 
     rules: { 
      'line1': "required", 
      'line2': "required", 
      'suburb': "required", 
      'state' : "required", 
      'country':"required", 


     }, 
     messages: { 

      'line1':{required: "Please enter supplier's address."}, 
      'line2':{required: "Please enter supplier's address."}, 
      'suburb':{required: "Please enter supplier's suburb."}, 
      'state':{required: "Please select a state"}, 
      'country':{required: "Please enter country"}, 
      'postcode':{required: "Please enter postcode"}, 

     }, 

     highlight: function (element) { 
      $(element).parent().parent().removeClass("success").addClass("error"); 
     }, 

     unhighlight: function (element) { 
      $(element).parent().parent().removeClass("error").addClass("success"); 
     } 
    }); // validate 
}); 

對於HTML

{{ form_widget(form.submit,{'attr':{'class':'btn btn-primary btn-large btn-style','value':'Save changes'} }) }} 
it generate 

<button id="ovc_bundle_productbundle_supplier_submit" class="btn btn-primary btn-large btn-style" value="Save changes" name="ovc_bundle_productbundle_supplier[submit]" type="submit">Save/Update Details</button> 
+0

我想你點擊兩次 –

+0

無我點擊一次 – vijaykumar

+0

add e.returnValue = false; –

回答

3

試試這個,以防止萬一多次結合,

$('#supplierForm').off('submit'); 
$('#supplierForm').on('submit', function(e) { 
      e.preventDefault(); 
      if ($(this).valid()) { 
       $.ajax({ 
        async: false, 
        data: $("#supplierForm").serialize(), 
        url: '{{ url('supplier_edit_new') }}', 
        type: 'POST', 
        success: function (data) { 
          window.location.reload(); 

        } 
       }); 
      } 
      return false; 
     }) 
+0

如果我們關閉提交。我不會提交一次? – vijaykumar

+0

你'關閉'提交和同一時間'再次'。所以它只關閉多個事件綁定。 –