layui checkbox 提交多个值的解决方法

吐槽一下,layui的checkbox简直就是一个坑...(不能提交数组)

数据是从后台来的

<div class="layui-form-item" >
     <label class="layui-form-label">品种</label>
     <div class="layui-input-inline">
          {foreach $quotation_type as $key=>$val}
               <input name="quotation_type" lay-skin="primary" value="{$key}" title="{$val}" type="checkbox">
          {/foreach}
      </div>
</div>

看看JS部分,有点绕,先把数据写进数组,然后,数组转成json格式,覆盖掉原先的data.field里面的数据

//获取checkbox数据
quotation = new Array();
$("input:checkbox[name='quotation_type']:checked").each(function(){
     quotation.push($(this).val());
});
var json = {};
for (var i = 0; i < quotation.length; i++) {
     json[i] = quotation[i];
}
let myJson = JSON.stringify(json);
data.field.quotation_type =  myJson ;

PHP部分,只需要把提交过来的json字符串转成数组就可以使用了

//提交的checkbox   提交过来的是json字符串
$data['quotation_type'] = json_decode($request->post('quotation_type'),true);

最后,进行你需要的数据库操作就可以了

马克一下

猜你喜欢

转载自blog.csdn.net/Gino_tkzzz/article/details/84315622