[英]How to get this form to submit only when it should
Probably an idiotic question but I find this really tricky: 可能是个白痴问题,但我发现这确实很棘手:
I am trying to make a form submit only when it specifically should. 我正在尝试仅在特定情况下才提交表单。 The script below executes every time I submit any other form in my page.
每当我在页面中提交任何其他表单时,下面的脚本就会执行。
How can I make it not execute, unless it is called for? 除非需要它,如何使它不执行?
$('#nurturing_pop_up_form').validate({
rules: {
emailaddress: {
required: true,
email: true
},
board_meeting: {
required: {
depends: function () {
return $('input[name=in_12_months]').is(':checked')==false && $('input[name=in_6_months]').is(':checked')==false
}
}
}
},
submitHandler: function(form) {
var url_remind = 'http://example.com';
var data_remind = $('#nurturing_pop_up_form').serializeArray();
$.ajax({
type: "POST",
url: url_remind,
data: data_remind,
success: function(){
$('.nurturing_pop_up').fadeOut();
}
});
}
});
Should I add something like this? 我应该添加这样的东西吗?
$('#nurturing_pop_up_form').submit(function(e){
e.preventDefault();
$('#nurturing_pop_up_form').validate({
rules: {
//the rest like above
Help is very very appreciated! 帮助非常感谢!
I am trying to make a form submit only when it specifically should.
我正在尝试仅在特定情况下才提交表单。
This process is handled automatically by jQuery Validate. jQuery Validate 自动处理此过程。 You should not be doing anything special to force this.
您不应该执行任何特殊操作来强制执行此操作。
The script below executes every time I submit any other form in my page.
每当我在页面中提交任何其他表单时,下面的脚本就会执行。
The script "below", .validate({...})
, is simply the plugin's initialization routine. 脚本“以下”
.validate({...})
只是插件的初始化例程。 Within the code you've shown, you are only initializing ONE form with id="nurturing_pop_up_form"
. 在显示的代码中,您仅使用
id="nurturing_pop_up_form"
初始化一个表单。 Other forms on the page would be completely unaffected. 页面上的其他表格将完全不受影响。 Perhaps you have invalid HTML, additional instances of
.validate()
, other submit handlers, or some erroneous code you have not shown us. 也许您有无效的HTML,
.validate()
其他实例,其他提交处理程序或一些未显示给我们的错误代码。
How can I make it not execute, unless it is called for?
除非需要它,如何使它不执行?
I think your question is based on an erroneous assumption of what's happening. 我认为您的问题是基于对所发生情况的错误假设。
.validate()
is only supposed to be called ONCE on page load (DOM ready handler) to initialize the plugin on your form. 仅在页面加载(DOM就绪处理程序)时才将
.validate()
称为ONCE来初始化表单上的插件。 The plugin then automatically captures the click, validates the data, and handles the submit. 然后,插件会自动捕获点击,验证数据并处理提交。
Should I add something like this?
我应该添加这样的东西吗?
$('#nurturing_pop_up_form').submit(function(e){
e.preventDefault();
$('#nurturing_pop_up_form').validate({...
Absolutely not. 绝对不。 You should never put
.validate()
within a .submit()
handler for the same form. 你不应该把
.validate()
一个内.submit()
处理程序相同的形式。 It's completely unnecessary, delays the plugin's initialization, and interferes with the built-in submit handler. 完全没有必要,它会延迟插件的初始化,并干扰内置的提交处理程序。
Nothing you've shown can explain how/why other forms would be affected. 您所显示的任何内容都无法解释其他形式将如何受到影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.