LayUI switch 开关ajax请求后更改状态
@if ($item['shop_status'] == 1) checked @endif lay-skin="switch"
lay-filter="switch_shop_status" lay-text="开启|关闭">
layui.use(['form', 'layer'], function () {
var form = layui.form
, layer = layui.layer;
// 状态
form.on('switch(switch_shop_status)', function (data) {
var check = data.elem.checked;
var shop_status = check ? '1' : '0';
var shop_id = $(data.elem).attr('data-shop_id');
var url = "{{url('/backend/shop/update')}}";
var ajax_data = {"shop_id": shop_id, "shop_status": shop_status};
var msg = '确定' + (shop_status == 1 ? '开启' : '关闭') + '态店铺吗?';
data.elem.checked = !check;
form.render();
layer.confirm(msg, function (index) {
$.ajax({
url: url,
method: "post",
dataType: "json",
data: ajax_data,
success: function (res, textStatus, xhr) {
if (res.status === 1) {
data.elem.checked = check;
layer.msg(res.msg, {icon: 1, time: 1000});
} else {
layer.msg(res.msg, {icon: 2, time: 1000});
}
form.render();
},
error: function (xhr, textStatus, error) {
layer.msg('请求失败', {icon: 2, time: 3000});
},
});
layer.close(index);
});
});
});