Jquery 表单验证提交处理程序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20616412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery form validation submit handler not working
提问by DJR
I have an existing application which uses JavaScript to do form validation and im trying to replace it with jQuery. My form has up to 20 fields and i am trying to validate only one field for now.
我有一个现有的应用程序,它使用 JavaScript 进行表单验证,我正在尝试用 jQuery 替换它。我的表单最多有 20 个字段,我现在只尝试验证一个字段。
This is the jQuery function i wrote to validate one field.
这是我为验证一个字段而编写的 jQuery 函数。
$(function() {
//$(".submit").click(function(){
// alert("jq");
$( "#serviceRtpLogMainForm" ).validate({
rules: {
p_incdate_in: "required"
},
messages: {
p_incdate_in: "Please enter the inception date."
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
}
//});
});
And below is how the form, submit and the field in question is being validated.
以下是表单、提交和相关字段的验证方式。
<html:form action="/serviceRtpLogMain" styleId="serviceRtpLogMainForm">
<html:text name = "serviceRptLog" property="p_incdate_in" styleId="incidDate" onfocus="cancelRequest1('DATE')" />
<A href="javascript:cal4.popup();">
<td valign="middle" align="left" width="100%" colspan="4" height=38 class="td3" >
<input type=submit value=Submit class="submit">
<input type=reset value=Reset>
</td>
Not my aim is to just validate if the field has been filled and to submit the form only if the field has been filled in the right format.
我的目的不是仅验证该字段是否已填写,并仅在以正确格式填写该字段时才提交表单。
What am i doing wrong here?
我在这里做错了什么?
回答by Subash Selvaraj
Make sure that your html will be like the below,
确保您的 html 如下所示,
<form id="serviceRtpLogMainForm" action="/serviceRtpLogMain">
<td valign="middle" align="left" width="100%" colspan="4" height="38" class="td3" >
<input type="text" name="p_incdate_in" class="p_incdate_in"/>
<input type="submit" value="Submit" class="submit"/>
</td>
</form>
and Script is
和脚本是
$(function() {
$( "#serviceRtpLogMainForm" ).validate({
rules: {
p_incdate_in: "required"
},
messages: {
p_incdate_in: "Please enter the inception date."
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
}
});
});
Check the Fiddle
检查小提琴
or If you dont want to use name attribute, add class attribute to your element and add rules like below,
或者如果您不想使用 name 属性,请将 class 属性添加到您的元素并添加如下规则,
$( "#serviceRtpLogMainForm" ).validate({
submitHandler: function(form) {
// do other things for a valid form
form.submit();
}
});
$(".p_incdate_in").rules("add", {
required:true,
messages:"Please enter the inception date."
});
Check Fiddle
检查小提琴