[英]Jquery form validation before Ajax request
我已經遍歷了其他線程,並已將其用作參考,但仍然找不到解決方案。
我的問題是:
我正在單擊按鈕時調用一個函數,現在該函數在驗證后具有驗證性。我正在嘗試在提交處理程序中使用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,則可以發布數據。 請建議我要去哪里錯了。
您可以像這樣嘗試調用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){}
});
}
}
將您的ajax data
語法更改為此。
data: {patientName:patientName},
確保在具有相同名稱“ PatientName”的捕獲方法上有一個參數,以從發布頁面上調用其發布。
這應該工作。
還要檢查您的帖子中是否獲得PatientName參數。 為此,請先通過“警報”檢查並傳遞PatientName參數。 您將知道自己得到了什么以及為什么不進行發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.