2013-07-03 94 views
0

我有一個表單,我使用jQuery驗證來驗證輸入是完整的。但是,發生的情況是驗證程序正在顯示消息,但它也正在對boxadd.php進行Ajax調用。我的印象是這個ajax調用只是在成功驗證後才做出的。當輸入沒有完成時,爲什麼它會達到Ajax事件?由於在驗證輸入之前驗證顯示成功

jQuery代碼

$(function() { 

    $.validator.setDefaults ({ 

     errorClass: 'form_error', 
     errorElement: 'div' 
    }); 

    $("#BA_boxform").validate({ 
     rules: { 
      BA_customer: { 
       required: true 
      }, 
      customerdept: { 
       required: true 
      }, 
      customeraddress: { 
       required: true 
      }, 
      BA_service: { 
       required: true 
      }, 
      BA_box: { 
       required: true 
      }, 
      BA_destdate: { 
       required: true 
      }, 
      BA_authorised: { 
       required: true 
      } 
     }, 
     messages: { 
      BA_customer: { 
       required: '<br />* required: You must select a customer' 
      }, 
      customerdept: { 
       required: "<br />* required: You must select a department" 
      }, 
      customeraddress: { 
       required: "<br />* required: You must select a customer address" 
      }, 
      BA_service: { 
       required: "<br />* required: You must select a service level" 
      }, 
      BA_box: { 
       required: "<br />* required: You must enter a box number for intake" 
      }, 
      BA_destdate: { 
       required: "<br />* required: You must enter a destruction date" 
      }, 
      BA_authorised: { 
       required: "<br />* required" 
      } 
     } 
    }); 

    $('#BA_boxform').on('submit', function() { 

     var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val(); 

     //alert(formdata); 
     $.ajax({ 
      type: "POST", 
      url: "/domain/admin/requests/boxes/boxesadd.php", 
      data: formdata, 
      dataType: 'json', 
      success: function (msg) { 
       //alert(msg); 
       if (typeof msg.boxerrortext !== "undefined" && msg.boxerrortext == "You must enter a box for intake") { 
        $("#BA_addbox").html(msg.boxerrortext); 
       } else { 
        $("#BA_addbox").html("You have successfully added box(es) " + '<span style="font-weight: bold;color:black;">' + msg.box + '</span>' + ' to the archive.' + '<br />' + 'You may now close this window or input more boxes.'); 
        $("#BA_boxform").get(0).reset(); 
       } 
       //$("#confirm_department").hide(); 

       /* 
       var $dialog = $('<div id="dialog"></div>') 
       .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.'); 
       $dialog.dialog({ 
       autoOpen: true, 
       modal: true, 
       title: 'Box intake submission successfull', 
       width: 400, 
       height: 200, 
       draggable: false, 
       resizable: false, 
       buttons: { 
       Close: function() { 
       $(this).dialog("close"); 
       } 
       } 
       }); 
       */ 
       //alert(msg); 
       //console.log(msg); 
       //$("#BA_addbox").html(msg.box); 

       //$("#formImage .col_1 li").show(); 
       //$("#BA_boxform").get(0).reset(); 
       //$("#boxaddform").hide(); 
      } 
     }); 
     return false; 
    }); 
}); 
+0

我不是100%確定,但是您是否必須致電驗證以確定它是否有效? – Liam

+0

@Liam我跟着YouTube的教程設置驗證和我的理解,有效不是強制性的,但我會研究它。謝謝 – user1532468

回答

0

只是檢查你讓Ajax調用你的消息是空了。

+0

對不起renish你可以詳細說明 – user1532468

+0

在ajax調用之前放一個條件,如果你的消息字符串你得到null或空,那麼你應該做一個Ajax調用否則不會。 –

+0

你可以用一個小提琴來舉例說明。謝謝 – user1532468