【问题标题】:jQuery - Uncaught Type Error: t.append is not a functionjQuery - 未捕获的类型错误:t.append 不是函数
【发布时间】:2016-09-02 14:39:08
【问题描述】:

我目前正在使用 jQuery Validate 和 Ajax 提交表单。当我提交这个时,没有任何反应并且页面重新加载 - 因为 .append() 由于某种原因不是一个函数?

代码如下。非常感谢任何帮助。

var $contact_form = $('#enquiry-form');
$contact_form.validate({
    errorPlacement: function() {
        return false;
    },
    submitHandler: function($contact_form) {
        $contact_form.append('<img class="loading" src="../../images/site/loading.gif">');

        $.ajax({
            type: "POST",
            url: "../../ajax/contact-form-send.php",
            data: $contact_form.serialize(),
            success: function(response) {

                var resp = JSON.parse(response);

                $contact_form.find('input').remove();
                $contact_form.find('textarea').remove();
                $contact_form.append('<p class="status-code ' + resp.status + '">' + resp.msg + '</p>');
            },
            error: function() {
                console.log('Ajax request not received');
            }
        });

        return false; //Stop the redirect after submission via ajax.
    }
});

【问题讨论】:

  • 您将 $contact_form 重新定义为 submitHandler 参数,该参数是 DOM 节点,而不是 jq 对象
  • 好的,好的。抱歉,我不确定我需要具体更改什么?
  • 好吧,你可以删除它:submitHandler: function() {...}
  • 事件处理程序接收event 作为第一个参数。只需从submitHandler: function($submit_form) 行中删除$submit_form,因为此变量将是事件或jqEvent。
  • 对,明白了。非常感谢大家 - 这似乎奏效了!

标签: javascript jquery ajax jquery-validate


【解决方案1】:

开发者为代表表单对象的 submitHandler 函数提供了一个参数。 submitHandler: function(form) {... 这样你就可以这样使用了...data: $(form).serialize()

var $contact_form = $('#enquiry-form');
$contact_form.validate({
    errorPlacement: function() {
        return false;
    },
    submitHandler: function() {
        $contact_form.append('<img class="loading" src="../../images/site/loading.gif">');

        $.ajax({
            type: "POST",
            url: "../../ajax/contact-form-send.php",
            data: $contact_form.serialize(),
            success: function(response) {

                var resp = JSON.parse(response);

                $contact_form.find('input').remove();
                $contact_form.find('textarea').remove();
                $contact_form.append('<p class="status-code ' + resp.status + '">' + resp.msg + '</p>');
            },
            error: function() {
                console.log('Ajax request not received');
            }
        });

        return false; //Stop the redirect after submission via ajax.
    }
});

【讨论】:

  • 更正确...开发人员为表示表单对象的 submitHandler 函数提供了一个参数。 submitHandler: function(form) {... 那么你可以这样使用它...data: $(form).serialize()
  • 如果你想在你的答案中抄袭我的评论,至少花点时间编辑你自己的代码块来显示它在说什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多