如何在WP前台为AJAX添加“等待”图标?

时间:2013-07-16 作者:Derfder

这是我的搜索js代码,用于处理搜索:

jQuery(document).ready(function($){

    $(\'#searchsubmit\').click(function(e){ 
        e.preventDefault();

        var searchval = $(\'#s\').val(); // get search term

        $.post(
            WPaAjax.ajaxurl,
            {
                action : \'wpa56343_more\',
                searchval : searchval
            },
            function( response ) {
                $(\'#results\').empty();
                $(\'#results\').append( response );
            }
        );
    });

});
我的问题是如何在搜索按钮(#searchsubmit)附近添加等待图标(WP admin中使用的图标),以便用户在单击后知道它正在工作?最好的方法是在我发布的现有js代码中只使用javasccript。

4 个回复
SO网友:NJENGAH

可以使用内置微调器类:

将类添加到希望微调器显示的HTML中,例如在搜索按钮之后:

<button type="button" id="searchsubmit">Search Button</button>
<span class="spinner"></span> <!-- Add this spinner class where you want it to appear--> 
在jQuery中,您应该添加is-active class 单击事件后立即发送到微调器;然后在您的ajax帖子回复中remove the is-active class :

jQuery(document).ready(function($) {

    $(\'#searchsubmit\').click(function(e){ 

        e.preventDefault();

         $(".spinner").addClass("is-active"); // add the spinner is-active class before the Ajax posting 

        $.post(
            WPaAjax.ajaxurl,
            {
                action : \'your_action\',
                data : data
            },
            function( response ) {

                 $(".spinner").removeClass("is-active"); // remove the class after the data has been posted 

            }
        );
    });

});
希望这有帮助。See more 来自WordPress核心文档

SO网友:Jake
jQuery(document).ready(function($) {

    $(\'#searchsubmit\').click(function(e){ 
        e.preventDefault();

        var $button = $(this);
        $button.addClass(\'disabled\').after(\'<div class="load-spinner"><img src="http://domain.com/path/to/image.gif" /></div>\');

        var searchval = $(\'#s\').val(); // get search term

        $.post(
            WPaAjax.ajaxurl,
            {
                action : \'wpa56343_more\',
                searchval : searchval
            },
            function( response ) {
                $(\'#results\').empty();
                $(\'#results\').append( response );

                $button.removeClass(\'disabled\');
                $(\'.load-spinner\').remove();
            }
        );
    });

});
SO网友:kkemple

jQuery(document).ready(function($){

    $(\'#searchsubmit\').click(function(e){ 
        e.preventDefault();
        var self = $( this );

        var loaderContainer = $( \'<span/>\', {
            \'class\': \'loader-image-container\'
        }).insertAfter( self );

        var loader = $( \'<img/>\', {
            src: GET_YOUR_BASE_URL + \'/wp-admin/images/loading.gif\',
            \'class\': \'loader-image\'
        }).appendTo( loaderContainer );


        var searchval = $(\'#s\').val(); // get search term

        $.post(
            WPaAjax.ajaxurl,
            {
                action : \'wpa56343_more\',
                searchval : searchval
            },
            function( response ) {
                $(\'#results\').empty();
                $(\'#results\').append( response );
                loaderContainer.remove();
            }
        );
    });
});
现在您所要做的就是获得指向wp admin的url的起始路径,代码应该可以正常工作。希望这有帮助。

SO网友:Mahesh Barot
$(function($)
{
    $("#btnSubmit").click(function () 
    {
        
        //Set the Valid Flag to True if one RadioButton from the Group of RadioButtons is checked.
        var isValid = $("input[name=gender]").is(":checked");   
        //Display error message if no RadioButton is checked.
        $("#spnError")[0].style.display = isValid ? "none" : "block";
    }),

    $(\'#contact_form\').validate(
    {
        rules:
        {
            uname:
            {
                required:true,
                minlength:3,
                maxlength:15
            },
            uemail:
            {
                required:true,
                email: true,
                minlength:3,                    
            },
            upass:
            {
                required:true,
                minlength:3,
                maxlength:15
            },
            cpass:
            {
                required:true,
                equalTo:\'#inputpassword\'
            },
            uaddres:
            {
                required:true,
                minlength:3,
                maxlength:100
            },
            gender:
            {
                required:true
            },
            ucity:
            {
                required:true,
                minlength:2,
                maxlength:15
            },
            ustate:
            {
                required:true
            },
            uzip:
            {
                required:true,
                minlength:6,
                maxlength:6 
            }
        
        },
        messages:
        {
            gender:
            {
                required:\'\'
            }
        },
        submitHandler:function(form)
        {
            event.preventDefault();

            var nameid  = $("#inputname").val();
            var emailid = $("#inputemail").val();               
            var passid = $("#inputpassword").val();
            var addid = $("#inputaddress").val();
            var genderid=$(\'#inputgender[name="gender"]:checked\').val();
            var cityid = $("#inputcity").val();
            var stateid = $("#inputstate").val();
            var zipid = $("#inputzip").val();

            var link="<?php echo admin_url(\'admin-ajax.php\'); ?>"
            var form_data=$(\'#contact_form\');
            
            $.ajax({
                    url: link,
                    type: \'post\',
                    data: 
                    {
                        action:\'contact_form_data\',
                        data:form_data.serialize()
                    },
                    beforeSend:function()
                    {
                            $(\'.loader\').show();
                    },
                    success: function (response) 
                    {
                        msg="<div>"+response+"</div>";
                        $(\'#msg\').html(msg);

                        console.log(response);
                        $("#contact_form")[0].reset();
                        $(\'.loader\').hide();
                    }
                });

        }

    });
});
结束

相关推荐

Rollover Image jQuery help

我基本上希望有一个图像的小缩略图,当用户将鼠标放在图像上时,我希望弹出一个更大的版本。我在谷歌上找到了这个tutorial.我无法理解代码。我应该把它放在哪个文件中?jQuery(function($) { <--This is the enclosing function $(document).ready(function(){ <--This is the overall jQuery function