【问题标题】:jQuery validate with AJAX in submitHandler, submits on second click?jQuery在submitHandler中使用AJAX验证,第二次点击提交?
【发布时间】:2013-08-16 04:07:49
【问题描述】:

我是 AJAX 的新手,并在此处使用此 SO 答案中的代码 jQuery Ajax POST example with PHP 与 WordPress 网站上的表单集成。它工作得很好,但我在将它与 jquery 验证集成时遇到了麻烦

我尝试将上面页面中的 javascript 放入下面的 submitHandler 函数中

$("#my-form").validate({
  submitHandler: function(form) {
    **js from other page**
  }
});

我的表单在第一次点击时验证。然后,如果我输入输入并提交没有任何反应,我必须再次单击表单才能使用 AJAX 正确提交。下面是一个jsfiddle。感谢任何帮助。

我的代码中的jsfiddle 认为它会向控制台记录一个错误,因为 form.php 没有链接

【问题讨论】:

  • **js from other page** 应该是什么?请显示足够的代码以对问题进行简明演示。您的 OP 不应依赖外部链接来显示所有相关代码。 jQuery Validate 插件只需要 one 单击,除非您错误地将 .validate() 包装在 clicksubmit 处理程序中。在您的情况下,您错误地将submit 处理程序包装在插件的默认submitHandler 内。由于插件的submitHandler 回调已经捕获了提交事件,所以你封闭的submit 处理程序是多余的。

标签: ajax jquery jquery-validate


【解决方案1】:

submitHandler 的工作是提交表单,而不是注册表单提交事件处理程序。

在触发 formm 提交事件时调用 submitHandler,在您的情况下,您注册了提交处理程序而不是提交表单,因此当第一次触发表单提交事件时,不会提交表单。当它第二次触发时,首先提交事件由验证器处理,然后触发您注册的处理程序,触发 ajax 请求。

在 submitHandler 中你只需发送 ajax 请求,无需注册事件处理程序

$("#add-form").validate({
    submitHandler: function (form) {
        // setup some local variables
        var $form = $(form);
        // let's select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");
        // serialize the data in the form
        var serializedData = $form.serialize();

        // let's disable the inputs for the duration of the ajax request
        $inputs.prop("disabled", true);

        // fire off the request to /form.php

        request = $.ajax({
            url: "forms.php",
            type: "post",
            data: serializedData
        });

        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {
            // log a message to the console
            console.log("Hooray, it worked!");
            alert("success awesome");
            $('#add--response').html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Well done!</strong> You successfully read this important alert message.</div>');
        });

        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown) {
            // log the error to the console
            console.error(
                "The following error occured: " + textStatus, errorThrown);
        });

        // callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {
            // reenable the inputs
            $inputs.prop("disabled", false);
        });

    }
});

【讨论】:

  • Arun 我刚刚检查了这段代码,我的输入值没有发送到 form.php 文件。使用验证返回旧代码。您的代码中的一些更改没有 var 请求和函数(表单)而不是事件。这些更改是否会导致我的输入无法发送?谢谢
  • 这最终工作submitHandler: function (form) {var $form = $(form); 否则我得到一个表单未定义错误
  • 如果我的 js 代码位于 js/custom.js 位置,那么 url 参数的 URL(这里是 url:forms.php)应该是什么?
  • .validate() 是一个插件吗?我在文档中找不到任何关于它的信息。
【解决方案2】:

调用$("#add-form").submit(function(){...}) 不会提交表单。它绑定了一个处理程序,该处理程序说明用户提交表单时要做什么。这就是为什么你必须提交两次:第一次调用 validate 插件的提交处理程序,它验证数据并运行你的函数,第二次调用你第一次添加的提交处理程序。

不要将代码包装在.submit() 中,直接在submitHandler: 函数中进行。变化:

var $form = $(this);

到:

var $form = $(form);

您不需要event.PreventDefault(),验证插件也可以为您完成。

【讨论】:

    【解决方案3】:
    $("#add-form").validate({
        submitHandler: function (form) {
            var request;
            // bind to the submit event of our form
    
    
    
            // let's select and cache all the fields
            var $inputs = $(form).find("input, select, button, textarea");
            // serialize the data in the form
            var serializedData = $(form).serialize();
    
            // let's disable the inputs for the duration of the ajax request
            $inputs.prop("disabled", true);
    
            // fire off the request to /form.php
    
            request = $.ajax({
                    url: "forms.php",
                    type: "post",
                    data: serializedData
            });
    
            // callback handler that will be called on success
            request.done(function (response, textStatus, jqXHR) {
                    // log a message to the console
                    console.log("Hooray, it worked!");
                    alert("success awesome");
                    $('#add--response').html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Well done!</strong> You successfully read this important alert message.</div>');
            });
    
            // callback handler that will be called on failure
            request.fail(function (jqXHR, textStatus, errorThrown) {
                    // log the error to the console
                    console.error(
                        "The following error occured: " + textStatus, errorThrown);
            });
    
                // callback handler that will be called regardless
                // if the request failed or succeeded
            request.always(function () {
                    // reenable the inputs
                    $inputs.prop("disabled", false);
            });            
    
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 2021-04-29
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多