简体   繁体   English

在WordPress上不起作用的jQuery代码

[英]jQuery code that doesn`t work on WordPress

I have a contact form that I submit via jQuery. 我有一个通过jQuery提交的联系表。 It works perfectly on HTML/PHP/JS template, but when I use it on a WordPress it doesn't work fine. 它可以完美地在HTML / PHP / JS模板上运行,但是当我在WordPress上使用它时,效果并不理想。

The form is submited, but the jquery code inside the submit function is like doesn't exit. 表单已提交,但是submit函数中的jquery代码好像没有退出。 It doesn't retrieve any error but the loader doesn`t appear and jquery validation doesn't occurs: 它不会检索任何错误,但是不会出现加载器,并且不会进行jquery验证:

<form method="post" action="<?php echo get_template_directory_uri() . '/templates/contact.php' ?>" name="contactform" id="contactform">
                <div class="col-sm-6">
                    <fieldset>
                        <input name="name" type="text" id="name" size="30" value="" placeholder="Name" />
                        <br />
                        <input name="email" type="text" id="email" size="30" value="" placeholder="Email" />
                        <br />
                        <input name="phone" type="text" id="phone" size="30" value="" placeholder="Phone" />
                        <br />
                    </fieldset>
                </div>
                <div class="col-sm-6">
                    <fieldset>
                        <textarea name="comments" cols="40" rows="8" id="comments" placeholder="Message"></textarea>
                        <button type="submit" class="btn btn-green-border btn-lg" id="submit" value="submit">Send Message</button>
                    </fieldset>
                </div>
            </form>

This is the jQuery: 这是jQuery:

// Send email 
    jQuery('#contactform').submit(function ($) {

        var action = $(this).attr('action');

        $("#message").slideUp(750, function () {
            $('#message').hide();

            $('#submit')
                .after('<img src="images/loader.gif" class="loader" />')
                .attr('disabled', 'disabled');

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                    verify: $('#verify').val()
                },
                function (data) {
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#contactform img.loader').fadeOut('slow', function () {
                        $(this).remove()
                    });
                    $('#submit').removeAttr('disabled');
                    if (data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

What can be the problem? 可能是什么问题?

    jQuery('#contactform').submit(function ($) {

尝试删除$ in function()

    jQuery('#contactform').submit(function () {

您的Wordpress安装可能使用了与您的代码不兼容的jQuery版本,请尝试包含更新的版本

I believe you are using your jquery inline. 我相信您正在使用内联的jQuery。

This is how I would do it (the correct way). 这就是我要做的(正确的方法)。

  1. Create a file in your themes js folder and call it something like contactform-js.js 在主题js文件夹中创建一个文件,并将其contactform-js.js

  2. Copy your jquery code and paste it into this new file. 复制您的jquery代码并将其粘贴到此新文件中。

  3. Register your new js file in your functions.php 在您的functions.php中注册新的js文件

This should sort some of your problems. 这应该可以解决您的一些问题。

function register_contact_script() {
    wp_enqueue_script( 'contact-script', get_template_directory_uri() . '/js/contactform-js.js', array( 'jquery' ), '' , true );
}

add_action( 'wp_enqueue_scripts', 'register_contact_script' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM