我正在尝试构建一个数据数组,然后使用 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, else if, else 部分工作正常,因为我可以看到警报。
当我尝试提醒数组长度“profilePhotoArray”时,它显示为 0,所以我没有正确填充数组。我需要使用 .push() 吗?我认为这种格式可以吗?
在通过 ajax 发送到 php 之前,我还需要对数组做任何事情吗?
谢谢你
** 编辑 - 我正在添加“profilePhotoArray[this.id+'_'+this.id] = action;” this.id 两次只是为了证明这个词,因为我将传递第二个这样的变量......我最好为此使用 JSON 吗?