JQ获取批量图片src的方法 / JQ批量设置json数组实现排序功能

——————批量获取图片地址——————

前端:

//获取和设置数据
var
photoList = []; $('.photo-list img').each(function(){ photoList.push($(this).attr('src')); })
//数据格式
data: {
photo_list: photoList,
contacts: contacts,
},

 

后台:

$photoList = request()->only('photo_list');  //得到的是二位数组
$photoList = $photoList['photo_list'];    //获取一维数组
$length = count($photoList);   //获取总数
$photo_list = [];  //定义数组
for ($i = 0; $i < $length; $i++) {
    $photo_list[$i] = [
        'repair_id' => $repairId,
        'url' => $photoList[$i],
    ];
}

 

 

 

 ——————批量获取class相同的数据数组——————

var serviceTitleArr = [];
$('.service-title').each(function(){
        serviceTitleArr.push($(this).val());
});
console.log(serviceTitleArr);

 

 

 

 

——————批量获取和设置json二维数组(带key的)——————

前端:

//获取和设置二维数组
var sequenceList = [];
$('.sequence').each(function(){
    var sequences = {};
    sequences.activity_id = $(this).attr('idx');
    sequences.sequence = $(this).val();
    sequenceList.push(sequences);
})

//数据格式
data: {
    sequence_list: sequenceList,
},

 

控制器:

$sequenceList = $_POST['sequence_list'];
$sequenceArr = [];
foreach ($sequenceList as $key => $value){
    $sequenceInfo = [
        'activity_id' => $value['activity_id'],
        'sequence' => $value['sequence'],
    ];
    array_push($sequenceArr,$sequenceInfo);
}
$res = (new ActivityModel())->addAll($sequenceArr);
return $res;

 

模型层:

public function addAll($sequenceArr){
        $res = (new Activity())->saveAll($sequenceArr);
        //return $this->getLastSql();
        if ($res) {
            return 'success';
        }else{
            return '排序失败';
        }
    }

 

posted @ 2017-12-22 20:21  小寒1206  阅读(639)  评论(0编辑  收藏  举报