2016-12-28 58 views
1

我已經通過其他線程,並使用一個供參考,但仍然無法找到解決方案。
我的問題是:Ajax請求之前的Jquery表單驗證

我呼籲按一下按鈕的功能,現在該功能驗證我嘗試後使用Ajax請求提交處理數據之後的驗證,問題是我的領域越來越驗證,但Ajax請求不調用。

<input type="button" value="Register" id="registerP" onclick="registerPatient()" class="form-control input-sm btn-success "> 

function registerPatient(){ 
$("#patientRegistration").validate({ 
rules: { 
    patientName: { 
    required: true, 
    textOnly: true 
       }, 
     }, 
messages: { 
     patientName: { 
     required: "Please enter the full name",     
        }, 
      }, 
submitHandler: function(form) { 
      $.ajax({ 
      type: "POST", 
      url: "/LoginMavenSpringMVC/appointment/savePatient", 
      data: "patientName=" + patientName, 
     success: function(response){}, 
     error: function(e){} 
     }); 
    } 
}); 
} 

但是,如果我不使用驗證和直接調用Ajax我能夠發佈數據。請提出我要去哪裏錯誤。

+0

給予網址或上傳完成,這樣就可以清楚地看到 –

+0

我不力明白你的意思。 –

回答

2

你可以嘗試這樣它調用jQuery的表單驗證,並檢查確認:

$(document).ready(function(){ 
    $("#patientRegistration").validate({ 
    rules: { 
     patientName: { 
     required: true, 
     textOnly: true 
        }, 
      }, 
    messages: { 
      patientName: { 
      required: "Please enter the full name",     
         }, 
       } 
    }); 
}); 

function registerPatient(){ 
    var IsValid=$("#patientRegistration").valid(); 
    if(IsValid){ 
    var patientName=""; //value for patient name 
    $.ajax({ 
      type: "POST", 
      url: "/LoginMavenSpringMVC/appointment/savePatient", 
      data: {"patientName": patientName}, 
      success: function(response){}, 
      error: function(e){} 
    }); 
    } 
} 
+0

感謝您分享代碼。 –

+0

您的歡迎:) –

0

將您的data語法更改爲此。

data: {patientName:patientName}, 

確保您有醒目的同名「patientName」的方法,從頁面調用其職位上後的參數。

這應該工作。

同時檢查您是否在您的帖子中獲得了patientName參數值。首先檢查「alert」並傳遞patientName參數。你會知道你正在得到什麼,爲什麼沒有發生帖子。

+0

如果我沒有使用驗證碼,那就是保存數據,即當調用函數時調用ajax。這意味着這個代碼Ajax沒有被調用,否則它會給我在控制檯中的錯誤。 –