我正在尝试建立一个数据数组,然后使用post to php将其变成ajax-以下是我的代码:
$('#mainBodySaveSubmitButtonProfilePhotoIMG').click(function() {
var profilePhotoArray = [];
$('.mainUnapprovedProfilePhotoWrapperDIV').each(function() {
var action = '';
alert( this.id );
if($('.mainUnapprovedProfilePhotoAttractiveIMG', this).is(':visible')) {
alert('attractive...');
action = 'attractive';
}
else if($('.mainUnapprovedProfilePhotoDeleteIMG', this).is(':visible')) {
alert('delete...');
action = 'delete';
}else{
alert('normal...');
action = 'normal';
}
profilePhotoArray[this.id+'_'+this.id] = action;
});
alert(profilePhotoArray.length);
for (i=0;i<profilePhotoArray.length;i++) {
console.log("Key is "+i+" and Value is "+array[i]);
}
$.post('scripts/ajax/ajax_approval_functions.php', {
'approvalProfilePhotos': '1',
'approvalProfilePhotosData': profilePhotoArray},
function(data) {
alert(data);
});
});
如果可以看到警报,则if,elif,if,else部分工作正常。
当我尝试警告数组长度'profilePhotoArray'时,它说为0,所以我没有正确填充数组。我需要使用.push()吗?我认为这种格式还可以吗?
另外我是否需要对数组做任何事情,然后再通过Ajax发送到php?
谢谢你
**编辑-我要添加“ profilePhotoArray [this.id +'_'+ this.id] =操作;” this.id两次只是为了证明这句话,因为我将传递第二个这样的变量……我最好为此使用JSON吗?
Javascript数组使用数字索引,因此您的存储失败。使用javascript对象存储基于字符串的密钥。
var lang=new Object();
lang["foo"]="Foo";
lang["bar"]="Bar";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句