【问题标题】:Wordpress post-only ajax form gives jQuery error?Wordpress 仅发布 ajax 表单给出 jQuery 错误?
【发布时间】:2019-04-06 01:52:14
【问题描述】:

我有一个带有 ajax 时事通讯注册表单的 wordpress 网站。在主页上,一切运行都没有错误:-) 在博客文章页面上,我收到此错误:

[错误] TypeError:e.indexOf 不是函数。 (在 'e.indexOf(" ")' 中,'e.indexOf' 未定义) (匿名函数) (jquery-3.3.1.min.js:2:31048)

[Error] TypeError: a.parent("a").size 不是函数。 (在 'a.parent("a").size()' 中,'a.parent("a").size' 未定义) (匿名函数) (jquery-3.3.1.min.js:2:31048)

HTML:

<div class="newsletter-section">
        <div id="form-messages"></div>

    <div class="form-div d-flex justify-content-center">
        <form id="ajax-contact" method="POST" action="mailer.php">

            <input type="text" class="" id="email" name="email" required placeholder="Sign up for our newsletter">
            <div class="btn-center">
                <button type="submit">Sign up</button>
            </div>
        </form>

    </div>  
</div>

代码:

$(function() {

// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$(form).submit(function(e) {
    // Stop the browser from submitting the form.
    e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    })
    .done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('error');
        $(formMessages).addClass('success');

        // Set the message text.
        $(formMessages).text(response);

        // Clear the form.
        $('#email').val('');
    })
    .fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');

        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! Er loopt iets fout. Je boeking is niet verzonden.');
        }
    });

});

});

谁能帮帮我?

谢谢, 弗雷德

【问题讨论】:

  • 您能发布ae 是什么以及这些元素的html 吗?基本上,错误是告诉您您正在尝试调用 indexOf(),但在调用时不存在。
  • 谢谢@Bardicer 我添加了 HTML 和 JS。
  • 并添加了代码@Taplar !

标签: jquery wordpress-theming ajaxform


【解决方案1】:

看来我必须在表单中提供 php 脚本的绝对路径。它完美无缺!感谢您的关注。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-25
    • 2014-07-04
    • 2011-05-21
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2017-02-05
    相关资源
    最近更新 更多