javascript Jquery Validator valid() 函数不适用于提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30846715/
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 Validator valid() function not working on submit
提问by Suvojit
We have multiple forms within a single page application. Individual forms have their individual save button on which individual form gets validated and if valid (using jquery validator .valid() method) then record gets saved in Database which is working fine.
我们在单个页面应用程序中有多个表单。单个表单有其各自的保存按钮,单个表单在该按钮上得到验证,如果有效(使用 jquery 验证器 .valid() 方法),则记录将保存在数据库中,该按钮工作正常。
Problem we are facing here is that when in the last form which is the final submit, when we try to validate all other forms for its validation using .valid() method, it always returns true irrespective of the wrong validations.
我们在这里面临的问题是,当在最后提交的最后一个表单中,当我们尝试使用 .valid() 方法验证所有其他表单以进行验证时,无论验证是否错误,它总是返回 true。
$("#submitForm").submit(function(event) {
$("#educationForm").validate();
console.log($("#educationForm").valid());
});
Here console also gives true even if the education Form is invalid
即使教育表格无效,这里的控制台也会给出 true
Kindly help us with this issue.
请帮助我们解决这个问题。
Regards, Suvojit
问候, 苏沃吉特
回答by Sparky
valid() function not working on submit
valid() 函数在提交时不起作用
The .valid()
method was designed to programmatically trigger a validation test. When you click "submit", the plugin is automatically doing a validation test, so there's no point in also calling .valid()
on the "submit" event.
该.valid()
方法旨在以编程方式触发验证测试。当您单击“提交”时,插件会自动进行验证测试,因此也无需调用.valid()
“提交”事件。
I think you may be misunderstanding the basic usage...
我想你可能误解了基本用法......
$("#submitForm").submit(function(event) {
$("#educationForm").validate(); // <- INITIALIZES PLUGIN
console.log($("#educationForm").valid()); // <- TEST VALIDATION
});
You would neverput
.validate()
inside of an event handler other than a DOM ready handler. The.validate()
method is how the plugin is initialized.After it's initialized properly, the plugin automaticallycaptures the click event of the submit button.
除了 DOM 就绪处理程序之外,您永远不会将
.validate()
事件处理程序放入事件处理程序中。 该.validate()
方法是插件的初始化方式。正确初始化后,插件会自动捕获提交按钮的点击事件。
By putting it inside of a .submit()
handler, you're interfering with the plugin, which is already trying to capture the exact same event.
通过将它放在.submit()
处理程序中,您会干扰插件,该插件已经在尝试捕获完全相同的事件。
Put inside of the DOM ready event handler instead...
而是放在 DOM 就绪事件处理程序中......
$(document).ready(function() {
$("#educationForm").validate(); // <- INITIALIZES PLUGIN
console.log($("#educationForm").valid()); // <- TEST VALIDATION
});
Working DEMO: http://jsfiddle.net/2vugwyfe/
工作演示:http: //jsfiddle.net/2vugwyfe/
OR: http://jsfiddle.net/2vugwyfe/1/
或:http: //jsfiddle.net/2vugwyfe/1/
The validation test is automatically triggered by any type="submit"
element.
验证测试由任何type="submit"
元素自动触发。
If you need to trigger a validation test by clicking another type of element, then use a custom .click()
handler...
如果您需要通过单击其他类型的元素来触发验证测试,请使用自定义.click()
处理程序...
$(document).ready(function() {
$("#educationForm").validate(); // <- INITIALIZES PLUGIN
$("#button").on('click', function() {
$("#educationForm").valid(); // <- TEST VALIDATION
});
});
DEMO 2: http://jsfiddle.net/pdhvtpdq/
演示 2:http: //jsfiddle.net/pdhvtpdq/
EDIT:
编辑:
To check if a different form is valid, use the .valid()
method inside of the submitHandler
option.
要检查不同的表单是否有效,请使用选项.valid()
内的方法submitHandler
。
In this example, when the submitHandler
for "Form A" fires, it then checks if "Form B" is valid before allowing a submission.
在此示例中,当submitHandler
for "Form A" 触发时,它会在允许提交之前检查 "Form B" 是否有效。
$(document).ready(function() {
$("#form_A").validate({ // initialize plugin on Form A
submitHandler: function(form) { // fires when Form A is valid
if($("#form_B").valid()) { // tests if Form B is valid
$(form).submit(); // submit Form A
}
}
});
});