Javascript jquery验证插件的问题
我正在使用一个jquery验证插件,我有下面的验证代码,这很麻烦,它只是一个规范,我希望错误应该如何显示在屏幕上Javascript jquery验证插件的问题,javascript,jquery,jquery-validate,Javascript,Jquery,Jquery Validate,我正在使用一个jquery验证插件,我有下面的验证代码,这很麻烦,它只是一个规范,我希望错误应该如何显示在屏幕上 jQuery(document).ready(function() { jQuery('form.CWvalidate').each(function() { var alertID = jQuery(this).attr('id'); jQuery(this).prepend('<div class="CWalertBox alertT
jQuery(document).ready(function() {
jQuery('form.CWvalidate').each(function() {
var alertID = jQuery(this).attr('id');
jQuery(this).prepend('<div class="CWalertBox alertText validationAlert" style="display:none;"></div><div class="CWclear"></div>');
});
jQuery('form.CWvalidate select').change(function() {
jQuery(this).blur();
});
jQuery.validator.setDefaults({
focusInvalid: true,
onkeyup: false,
onblur: true,
errorClass: "warning",
errorElement: "span",
errorPlacement: function(error, element) {
jQuery(element).siblings('label').addClass('warning');
},
showErrors: function(errorMap, errorList) {
if (this.numberOfInvalids() > 0) {
var formID = jQuery(this.currentForm).attr('id');
if (formID == 'CWformCustomer') {
var validationMessage = 'Complete all required information';
} else if (formID == 'CWformLogin') {
var validationMessage = '';
} else if (formID == 'CWformOrderSubmit') {
var validationMessage = 'Complete all required information';
} else if (formID == 'CWformAddToCartWindow') {
var validationMessage = 'Select at least one item';
} else if (formID == 'CWformAddToCart') {
var validationMessage = 'Choose from below options before adding to cart';
} else {
var validationMessage = 'Complete your selection';
};
if (!(validationMessage == '')) {
jQuery(this.currentForm).find('.errormsg').show().html(validationMessage);
};
this.defaultShowErrors();
} else {
jQuery(this.currentForm).children('div.validationAlert').empty().hide();
jQuery(this.currentForm).children('span.warning').remove();
jQuery(this.currentForm).children('.warning').removeClass('warning');
}
}
});
jQuery('form.CWvalidate').each(function() {
jQuery(this).validate();
});
});
出了什么问题,我被卡住了,不知道发生了什么…您似乎在同一张表单上调用了两次
.validate()
在这里:
在这里:
$("#dataModification").validate({
submitHandler: function(form) {
...
只使用第一个实例,忽略任何后续实例,这就解释了为什么submitHandler
无法工作,表单只是刷新。它还解释了为什么它只在使用CWvalidate
类时失败
要解决此问题,您必须了解如何为每个表单调用.validate()
一次
一种可能的解决方法是将ajax URL放在表单
标记的操作
属性中,然后使用.attr('action')
在.ajax()
中检索此URL。然后您的submitHandler
可以足够通用,可以包含在setDefaults()
中
另请注意:此插件没有名为
onblur
的选项。它是onfocusout
,如果不中断插件,就无法将其设置为true
您似乎在同一表单上调用了两次.validate()
在这里:
在这里:
$("#dataModification").validate({
submitHandler: function(form) {
...
只使用第一个实例,忽略任何后续实例,这就解释了为什么submitHandler
无法工作,表单只是刷新。它还解释了为什么它只在使用CWvalidate
类时失败
要解决此问题,您必须了解如何为每个表单调用.validate()
一次
一种可能的解决方法是将ajax URL放在表单
标记的操作
属性中,然后使用.attr('action')
在.ajax()
中检索此URL。然后您的submitHandler
可以足够通用,可以包含在setDefaults()
中
另请注意:此插件没有名为
onblur
的选项。它是onfocusout
,如果不中断插件,就无法将其设置为true
控制台中有错误吗?你能点击任何断点吗?控制台中没有,没有,它基本上只工作了一半,我的意思是验证工作,但当它转到提交处理程序时,它只刷新整个页面。请显示相关的呈现HTML标记。控制台中有错误吗?你能点击任何断点吗?控制台中没有,没有,它基本上只工作了一半,我的意思是验证工作,但当它转到提交处理程序时,它只是刷新整个页面请显示相关的呈现HTML标记。
$("#dataModification").validate({
submitHandler: function(form) {
...
submitHandler: function(form) {
var datastring = $(form).serialize();
$.ajax({
type: "post",
url: $(form).attr('action'), ...