1
我正在为WordPress的插件工作,并在执行$.post
下面的js代码有问题。
的JS调用,表单验证发生时,表单输入正确序列化到数据后,该$.post
只是不会执行。
的形式正在从Admin发布,目前我不能得到.submit行动工作,所以我使用。点击执行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);
});
您在提交处理程序结束时错过了退货 – Triode
您能告诉我您的意思吗?我是js的新手,显然... –
好的,完美添加'返回false';并且一切都很好......除了使用提交时不能调用该函数,只有在使用点击处理程序时才能调用。不是我介意使用.click正确更新表单,但它很好找出为什么这不起作用.... –