简体   繁体   English

如何使此表单仅在应提交时提交

[英]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.

相关问题 如果用户专注于登录输入,如何在输入密钥时提交表单? - How do I get the form to submit on enter key only when user has focus on login input? 如何实现1个表单的2个提交按钮。 两者都应触发HTML5验证,但实际上只有一个应提交 - How to implement 2 submit buttons for 1 form. Both should trigger HTML5 validation, but only one should actually submit 更改后如何只提交表单上单个选择框的值? - How to submit only the value of a single select box on a form when changed? 如何只有所有字段的数据都有效才提交表单? - How to submit form only when data in all fields is valid? 仅在选择字段时如何允许表单提交 - how to allow a form submit only when fields are selected 按下提交按钮时如何更改表单的动作|| 使提交按钮只能单击一次? - how to change the action of a form when the submit button is pressed || make the submit button clickable only once? 通过JQuery提交表单时如何点击提交按钮 - How to get clicked submit button when submitting a form via JQuery 在Angular JS中提交表单时如何获取输入字段的标签 - How to get label of an input field when submit a form in Angular JS 在表单上按下提交时如何调用不同的get函数; 角 - How to call different get functions when submit is pressed on the form; angular jQuery - 如何在ajax化表单时获取提交类型输入的值 - jQuery - how to get the value of an input of type submit when ajaxifying a form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM