在Ajax请求之前进行JQuery表单验证

编程入门 行业动态 更新时间:2024-10-12 03:17:47
本文介绍了在Ajax请求之前进行JQuery表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经遍历了其他线程并使用了一个线程作为参考,但是仍然找不到解决方案. 我的问题是:

I have gone through other threads and used one for reference but still I am not able to find the solution. My question is:

我现在在按钮单击时调用一个函数,该函数在验证后具有验证性.我正在尝试在提交处理程序中使用ajax请求发布数据,问题是我的字段正在得到验证,但未调用Ajax请求.

I am calling a function on button click now that function has validation after validation I am trying to post data with ajax request in submit handler, problem is my fields are getting verified but Ajax request is not invoked.

<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,则可以发布数据.请建议我要去哪里错了.

However if I am not using validation and calling Ajax directly i am able to post the data. Please suggest where I am going Wrong.

推荐答案

您可以像这样尝试调用jquery表单验证并检查是否经过验证:

You can try like this which call jquery form validation and check if validated:

$(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请求之前进行JQuery表单验证

本文发布于:2023-11-24 20:00:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   Ajax   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!