97 lines
2.4 KiB
JavaScript
97 lines
2.4 KiB
JavaScript
// 格式化表单为json
|
|
$.fn.serializeJson = function() {
|
|
var o = {};
|
|
var a = this.serializeArray();
|
|
$.each(a, function() {
|
|
if (o[this.name]) {
|
|
if (!o[this.name].push) {
|
|
o[this.name] = [ o[this.name] ];
|
|
}
|
|
o[this.name].push(this.value || '');
|
|
} else {
|
|
o[this.name] = this.value || '';
|
|
}
|
|
});
|
|
return o;
|
|
};
|
|
// 如果是空值,则返回''
|
|
var isNull = function (data) {
|
|
if (data === null || data === undefined || data === '') {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
$(function () {
|
|
// 点击删除按钮时
|
|
$("form .del").on('click', function () {
|
|
console.log($(this).value());
|
|
layer.confirm("确定要删除此消息吗?", function () {
|
|
$.ajax({
|
|
url: $(this).value(),
|
|
type: 'post',
|
|
success: function (res) {
|
|
closeAndReload();
|
|
}
|
|
})
|
|
});
|
|
|
|
})
|
|
});
|
|
|
|
|
|
var ajaxSubmit = function (form) {
|
|
var $form = $(form);
|
|
$form.find('[required]').each(function () {
|
|
var label = $(this).parents('.layui-form-item').find('.layui-form-label');
|
|
label.html('<span style="color: red;">* </span>' + label.html());
|
|
});
|
|
|
|
$form.on('submit', function (e) {
|
|
// 禁用默认提交事件
|
|
e.preventDefault();
|
|
$.ajax({
|
|
url: $form.attr('action'),
|
|
dataType: 'json',
|
|
// async: false,
|
|
data: $form.serializeJson(),
|
|
type: 'POST',
|
|
success: function (req) {
|
|
console.log("121111");
|
|
alert(req.msg);
|
|
// 1 为失败 0 为成功
|
|
let code = req.code ;
|
|
if (code == 0) {
|
|
closeAndReload();
|
|
}
|
|
},
|
|
complete: function () {
|
|
//请求完成的处理
|
|
},
|
|
error: function (error) {
|
|
}
|
|
});
|
|
});
|
|
|
|
};
|
|
|
|
function closeAndReload() {
|
|
//刷新页面
|
|
location.reload();
|
|
//获取窗口索引
|
|
// var index = parent.layer.getFrameIndex(window.name);
|
|
// parent.layer.close(index);
|
|
}
|
|
|
|
|
|
layui.use(['laydate'], function(){
|
|
let laydate = layui.laydate;
|
|
// 设置日期
|
|
lay('.datetime').each(function(){
|
|
laydate.render({
|
|
elem:this,
|
|
// type: 'datetime'
|
|
format: 'yyyy-MM-dd'
|
|
});
|
|
});
|
|
});
|