吐槽一下,layui的checkbox簡直就是一個坑...(不能提交數(shù)組)
數(shù)據(jù)是從后臺來的
<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部分,有點(diǎn)繞,先把數(shù)據(jù)寫進(jìn)數(shù)組,然后,數(shù)組轉(zhuǎn)成json格式,覆蓋掉原先的data.field里面的數(shù)據(jù)
//獲取checkbox數(shù)據(jù)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字符串轉(zhuǎn)成數(shù)組就可以使用了
//提交的checkbox 提交過來的是json字符串$data['quotation_type'] = json_decode($request->post('quotation_type'),true);最后,進(jìn)行你需要的數(shù)據(jù)庫操作就可以了。
以上這篇解決layui checkbox 提交多個值的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。
新聞熱點(diǎn)
疑難解答
圖片精選