这是我的代码:
$('body').on('click', '.update_contact_profile', function (){
var url = $("#ajaxUrl").val();
var ids = $(this).closest("div").nextAll(".contact-extra-info").find(".contact-ids").attr("id");
ids = ids.split("-")
var contactId = ids[0];
var customerId = ids[1];
var postDataUpdate = [];
$(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){
if ($(this).val()) {
postDataUpdate[''+$(this).attr('id')+''] = $(this).val();
}
});
var request = $.ajax({
url: url,
method: "POST",
data: {
id : contactId,
contact : postDataUpdate,
form_key : FORM_KEY,
customerId : customerId
}
});
request.success(function( text ) { // replace the ajax_wrapper with the new one
$(".ajax_wrapper").replaceWith(text);
$("#contact_form").find('select').selectize();
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
我的问题是此var postDataUpdate
没有传递给ajax。在firebug上没有出现contact
。如果在我的Ajax请求之前进行console.log(postDataUpdate)
,我会得到数组。
那么对此有任何想法吗?
postDataUpdate
应该是一个对象,而不是数组:
[..]
var postDataUpdate = {};
$(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){
if ($(this).val()) {
postDataUpdate[''+$(this).attr('id')+''] = $(this).val();
}
});
[..]
检查此片段:
var asArray = [];
asArray[1] = "foo";
asArray["foo"] = "bar";
console.log("asArray:");
console.log(asArray);
var asObject = {};
asObject[1] = "foo";
asObject["foo"] = "bar";
console.log("asObject:");
console.log(asObject);