douchu2823 2017-03-30 06:38
浏览 13
已采纳

如何在ajax中拆分数组?

var rows_selected = [];
var rowId = data[0]; // conten [24,25,26,27 etc]
var namaFile = data[4]; // content [foto1.JPG, foto2.JPG, foto3.JPG, foto4.JPG ]

//If checkbox is checked and row ID is not in list of selected row IDs
rows_selected.push(rowId,namaFile);

var dataId=[];
$.each(rows_selected, function(index, rowId, namaFile){
  dataId.push(rowId,namaFile);
});

in ajax i sent dataId to controller

$.ajax({
   data: {"iddata": "[{"+dataId+"}]"},
});

but in controller iddata receive just one array like this 24,6a5236ee48848e648553a35d64c4f64dJPG,25,a31ea31db31483506fbd95e463d4c8ffJPG,26,042fd1eee499e5bb9c93853c0299b42eJPG,27,e243845bcc0447324d449e37b271c9ebJPG,28,8f331fa2a713f5268ab71698a65185b6JPG

some code in controller

$iddata=array();
    $iddata=array($_GET['iddata']);
    $a=explode(",", $iddata[0]);
    $b=preg_replace('/[^A-Za-z0-9\-]/', '', $a);
    $jumlahdata=count($b)/2;
    for($i=0;$i<$jumlahdata;$i++)
    {
        //delete on database
       $data=array('id_perjal'=>$b[$i]);
       $this->M_perjal_terpasang->Delete_Foto($data);
        //and i want deleting foto with unlink
       unlink("assets/images/uploads/"$b[$i]);
    }
  • 写回答

2条回答 默认 最新

  • dongtou8736 2017-03-30 08:42
    关注

    I would start by using a naming convention, and name arrays with plural words, so that you know that namaFiles (plural) is an array, and one element of it is called namaFile (singular). This will avoid some confusion.

    Let's say that two checkboxes are checked, and their IDs are different, then you would somehow add two rows to rows_selected. Currently, you push those values one after the other so that you get an array like [24, 'foto1.JPG', 27, 'foto4.JPG'], but you really want to get them grouped in pairs, like this:

    [{rowId: 24, namaFile: 'foto1.JPG'}, {rowId: 27, namaFile: 'foto4.JPG'}]
    

    So you should push the values like this (supposing you have an index):

    rows_selected.push( {rowId: rowIds[index], namaFile: namaFiles[index]} );
    

    This you would repeat for other pairs that need to be added.

    In your Ajax data, you should use a JSON string, like this:

    var ajaxobj = {
       data: {"iddata": JSON.stringify(rows_selected) },
    }
    

    On the server side you would convert the JSON back to an associative array, and then access the two elements of each pair by their key name:

    $iddata = $_GET['iddata'];
    $a = json_decode($iddata, true);
    foreach($a as $pair) {
       //delete in database
       $data = array('id_perjal' => $pair['rowId']);
       $this->M_perjal_terpasang->Delete_Foto($data);
       //delete foto with unlink
       unlink("assets/images/uploads/" . $pair['namaFile']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?