jquery validate 过早检查表单
我在表单上有一个 jquery 验证设置。 在填写表单时,我使用 ajax 2 个字段值来查看表单是否可接受,从而禁用/启用表单的提交按钮。
看起来结果是 - 表单已提前验证 - 这应该仅在点击提交按钮时发生。
有人可以建议修复吗?
谢谢!
这是一些代码:
//validate form:
$("#ftblSubGroupsadd").validate({
showErrors: function(){
this.defaultShowErrors();
alert('Note, a few mandatory fields remain un-answered.');
$("label.error").closest("div[class=inside]").css("display","block");
$("label.error").closest("div.boxed").removeClass().addClass('box');
}
});
//check input of 2 fields, while form is still being filled out:
$("#x_ShortFileName, #x_URL").change(function(){
var fileName = $("#x_ShortFileName").val();
var location = $("#x_URL").val();
var sendStr = "fileName="+fileName+"&location="+location;
//alert(sendStr);
$("#locationResponse").load("/system/checkFileExists.asp",sendStr, function(response, status, xhr){
var responseArr = response.split("|");
if (responseArr[0] == "error") {
//alert("probem");
$("#locationResponse").removeClass().addClass('red');
$("#locationResponse").html(responseArr[1]);
$("#btnAction").attr("disabled","disabled");
$("#finalURL").html(''); //(responseArr[2]);
} else {
//alert("all good");
$("#locationResponse").removeClass().addClass('green');
$("#locationResponse").html(responseArr[0]);
$("#btnAction").removeAttr("disabled");
$("#finalURL").html(responseArr[1]);
}
});
});
HTML 表单代码:
<form name="ftblSubGroupsadd" id="ftblSubGroupsadd" action="tblSubGroupsadd.asp" method="post" enctype="multipart/form-data">
<table class="ewTable">
<tr>
<td width="30%" class="ewTableHeader"><strong>Full Sub-Group Name</strong><span class='ewmsg'> *</span></td>
<td class="ewTableAltRow"><span id="cb_x_FullName">
<input type="text" name="x_FullName" id="x_FullName" size="30" maxlength="500" value="" class="required" title=" "></span></td>
</tr>
<tr>
<td class="ewTableHeader"><strong>Short file name</strong> or<strong> access code</strong><span class='ewmsg'> *</span><br />
<span class="grey">This will be used to create the file name </span></td>
<td class="ewTableAltRow"><span id="cb_x_ShortFileName">
<input type="text" name="x_ShortFileName" id="x_ShortFileName" size="30" maxlength="30" value="" class="required" title=" " />
</span>
<div id="locationResponse"></div></td>
</tr>
<tr>
<td class="ewTableHeader">Location of file<span class='ewmsg'> *</span><br />
<span class="grey">Such as: /<strong>groups</strong>/xxxx.asp</span></td>
<td class="ewTableAltRow"><span id="cb_x_URL">
<input type="text" name="x_URL" id="x_URL" size="30" maxlength="255" value="" class="required" title=" " />
<div id="finalURL" class="green"></div>
</span></td>
</tr>
<tr>
<td class="ewTableHeader">Display Program Dates? <span class="ewmsg"> *</span></td>
<td class="ewTableAltRow"><span id="cb_x_optDisplayProgramDates">
<input type="radio" name="x_optDisplayProgramDates" id="x_optDisplayProgramDates" value="0" class="required" title="*">
No <input type="radio" name="x_optDisplayProgramDates" id="x_optDisplayProgramDates" value="1" class="required" title="*">
Yes
</span></td>
</tr> </table>
<p align="center">
<input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
可能的 ajax 返回数据:
error|Warning, the file groups/hop.asp already exists!
或
The file name is available.|The location of your file will be: www.site.com/y/tyu.asp
i have a jquery validate setup on the form.
whil the form is being filled out, i ajax 2 of the field values to see if the form is acceptable or not, and as a result disable/enable the submit button of the form.
it seems like as a result - the form is premateurly validated - which shoudl only happen when the submit button is hit.
can anyone suggest a fix?
thanks!
Here is some code:
//validate form:
$("#ftblSubGroupsadd").validate({
showErrors: function(){
this.defaultShowErrors();
alert('Note, a few mandatory fields remain un-answered.');
$("label.error").closest("div[class=inside]").css("display","block");
$("label.error").closest("div.boxed").removeClass().addClass('box');
}
});
//check input of 2 fields, while form is still being filled out:
$("#x_ShortFileName, #x_URL").change(function(){
var fileName = $("#x_ShortFileName").val();
var location = $("#x_URL").val();
var sendStr = "fileName="+fileName+"&location="+location;
//alert(sendStr);
$("#locationResponse").load("/system/checkFileExists.asp",sendStr, function(response, status, xhr){
var responseArr = response.split("|");
if (responseArr[0] == "error") {
//alert("probem");
$("#locationResponse").removeClass().addClass('red');
$("#locationResponse").html(responseArr[1]);
$("#btnAction").attr("disabled","disabled");
$("#finalURL").html(''); //(responseArr[2]);
} else {
//alert("all good");
$("#locationResponse").removeClass().addClass('green');
$("#locationResponse").html(responseArr[0]);
$("#btnAction").removeAttr("disabled");
$("#finalURL").html(responseArr[1]);
}
});
});
HTML form code:
<form name="ftblSubGroupsadd" id="ftblSubGroupsadd" action="tblSubGroupsadd.asp" method="post" enctype="multipart/form-data">
<table class="ewTable">
<tr>
<td width="30%" class="ewTableHeader"><strong>Full Sub-Group Name</strong><span class='ewmsg'> *</span></td>
<td class="ewTableAltRow"><span id="cb_x_FullName">
<input type="text" name="x_FullName" id="x_FullName" size="30" maxlength="500" value="" class="required" title=" "></span></td>
</tr>
<tr>
<td class="ewTableHeader"><strong>Short file name</strong> or<strong> access code</strong><span class='ewmsg'> *</span><br />
<span class="grey">This will be used to create the file name </span></td>
<td class="ewTableAltRow"><span id="cb_x_ShortFileName">
<input type="text" name="x_ShortFileName" id="x_ShortFileName" size="30" maxlength="30" value="" class="required" title=" " />
</span>
<div id="locationResponse"></div></td>
</tr>
<tr>
<td class="ewTableHeader">Location of file<span class='ewmsg'> *</span><br />
<span class="grey">Such as: /<strong>groups</strong>/xxxx.asp</span></td>
<td class="ewTableAltRow"><span id="cb_x_URL">
<input type="text" name="x_URL" id="x_URL" size="30" maxlength="255" value="" class="required" title=" " />
<div id="finalURL" class="green"></div>
</span></td>
</tr>
<tr>
<td class="ewTableHeader">Display Program Dates? <span class="ewmsg"> *</span></td>
<td class="ewTableAltRow"><span id="cb_x_optDisplayProgramDates">
<input type="radio" name="x_optDisplayProgramDates" id="x_optDisplayProgramDates" value="0" class="required" title="*">
No <input type="radio" name="x_optDisplayProgramDates" id="x_optDisplayProgramDates" value="1" class="required" title="*">
Yes
</span></td>
</tr> </table>
<p align="center">
<input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
Possible ajax return data:
error|Warning, the file groups/hop.asp already exists!
or
The file name is available.|The location of your file will be: www.site.com/y/tyu.asp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,该插件会激活对用户输入的验证。您应该停用这些选项:
希望这有帮助,d。
编辑评论:
我不是 100%,但当所有错误都得到纠正时,似乎会再次调用处理程序(您自己通过 showErrors 选项定义的,或者默认的) 。完整的处理程序签名有两个参数:
第一个参数是错误映射,第二个参数是错误数组。
因此,最好的选择是检查错误数量:
By default, the plugin activates validation on user input. You should deactivate those options :
Hope this helps, d.
Edit for comments:
I'm not 100%, but it seems the handler (either your own defined via the showErrors option, or the default one) is called again when all errors have been corrected. The full handler signature has two parameters:
First argument is a map of the errors, second is an array of errors.
So your best bet would be to check the amount of errors: