【问题标题】:jQuery $.post not executing, how to fixjQuery $.post 没有执行,如何解决
【发布时间】:2014-03-28 20:04:54
【问题描述】:

我正在为 WordPress 开发插件,但在执行 $.post 时遇到以下 js 代码问题。

js被调用,表单验证发生,表单输入被正确序列化为post数据,$.post只是不执行。

表单正在从管理员发布,目前我无法让 .submit 操作工作,所以我使用 .click 来执行 js 功能。这可能与问题有关,我不确定...如果我使用 .submit 操作,而不是使用 .click 操作,表单将在不提交的情况下加载...以前从未遇到过这个问题,说起来很令人沮丧最少。

代码如下:

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

    $("#edit_member_submit").click( function() {

        // define

        var numbers = /^[0-9]+$/;

        var referrer_id = $("#referrer_id").val();

        // Validate fields START

        if( !referrer_id.match(numbers) ) {

            alert("Please enter a numeric value");

            return false;           

        }

        // Validate fields END       

        $("#ajax-loading-edit-member").css("visibility", "visible");

        // Convert to name value pairs          
        // Define a data object to send to our PHP      

            $.fn.serializeObject = function() {

                var arrayData, objectData;
                arrayData = this.serializeArray();
                objectData = {};

                $.each(arrayData, function() {
                    var value;

                if (this.value != null) {

                  value = this.value;

                } else {

                  value = '';

                }

                if (objectData[this.name] != null) {

                    if (!objectData[this.name].push) {

                    objectData[this.name] = [objectData[this.name]];

                    }

                    objectData[this.name].push(value);

                    } else {

                    objectData[this.name] = value;

                    }

                });

              return objectData;                    

            };          

        var data = $("#edit_member_form").serializeObject(); //the dynamic form elements.

        //alert(JSON.stringify(data));        

        data.action = "edit_member_info"; //the action to call
        data._ajax_nonce = custajaxobj.nonce; // This is the name of the nonce setup in the localize_script

        // Define the URL for the AJAX to call
        var url = custajaxobj.ajaxurl; 

        //alert( JSON.stringify( data ) );
        //alert( JSON.stringify( url ) );

        $.post(url, data, function(response) {

            $("#ajax-loading-edit-member").css("visibility", "hidden");

            alert(response);

        });

        return false;

    });

});

好像最后一节有问题:

$.post(url, data, function(response) {

        $("#ajax-loading-edit-member").css("visibility", "hidden");

        alert(response);

    });

【问题讨论】:

  • 您在提交处理程序结束时错过了返回
  • 你能告诉我你的意思吗?我是 js 新手,显然......
  • OK,完美添加'return false;'一切都很好......除了仍然无法使用提交调用函数,只有在使用点击处理程序时。我不介意使用 .click 正确更新表单,但最好弄清楚为什么这不起作用....

标签: javascript php jquery wordpress forms


【解决方案1】:
$.post( "ajax/test.html", function( data ) {

$("#ajax-loading-edit-member").css("visibility", "hidden");

    alert(data);

});

【讨论】:

    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 2013-07-15
    • 2014-04-01
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多