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正確更新表單,但它很好找出爲什麼這不起作用.... –