0

この記事http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/の wordpress codex で提案されているように、WordPress フロントエンドで ajax を使用しています。

この記事のすべてを正確にセットアップします。これは私が使用するコードです

$(document).ready(function(){



            jQuery("#jw_email").on("blur", function(){

            var jw_email = $(this).val();

                jQuery.post(
                    // see tip #1 for how we declare global javascript variables
                    MyAjax.ajaxurl,
                    {
                        // here we declare the parameters to send along with the request
                        // this means the following action hooks will be fired:
                        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
                        action : 'rcv-checkuser',

                        // other parameters can be added along with "action"
                        jw_email : jw_email,
                        postCommentNonce : MyAjax.postCommentNonce
                    },
                    function( response ) {
                         $("#userid").val(response);
                        console.log(response);
                    }
                );

            });





});

今私が抱えている問題は

jQuery("#jw_email").on("blur", function(){

上記の行のぼかしイベントは、ページが読み込まれるたびに1回発生し、関数 rcv-checkuser はぼかしイベントが発生する前に実行されます

しかし、2 回目からは、ぼかしも期待どおりに機能します。

ただし、ページが初めて読み込まれるとき、onblur は「ぼかし時」ではなく「読み込み時」に起動します。

これを防ぐ方法

私はこの解決策を試しましたが、機能していませ

4

1 に答える 1

0

あなたの要件が何であるかはわかりませんが、フィールドが空の場合でもこの ajax 呼び出しは発生しないはずなので、条件を入力してみてください

if(jQuery("#jw_email").val() != ''){

jQuery(this).on("blur", function(){

var jw_email = $(this).val();

    jQuery.post(
        // see tip #1 for how we declare global javascript variables
        MyAjax.ajaxurl,
        {
            // here we declare the parameters to send along with the request
            // this means the following action hooks will be fired:
            // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
            action : 'rcv-checkuser',

            // other parameters can be added along with "action"
            jw_email : jw_email,
            postCommentNonce : MyAjax.postCommentNonce
        },
        function( response ) {
             $("#userid").val(response);
            console.log(response);
        }
    );

});

}

ロード時にフィールドが空になるため、ajax呼び出しが停止すると思います

それが役に立てば幸い!

ありがとう

于 2013-09-05T08:19:15.140 に答える