/** * admin 后台js * */ //字符串为空 function isNull(str) { if (str == null || str == "" || str.length < 1) return true; else return false; } //获取 str 字符长度,中文单字2字符,英文1字符F function getblen(str) { var l = str.length; var blen = 0; for (i = 0; i < l; i++) { if ((str.charCodeAt(i) & 0xff00) != 0) { blen++; } blen++; } return blen; } //textarea 字数提示 function textarealength(obj, toplength) { var note = $(obj).val(); var blen = getblen(note); if (blen <= toplength) { $(".textarea-length").html(blen); } else { $(".textarea-length").css('color', 'red'); $(".textarea-length").html(blen); } } // js 正则验证邮箱 function testEmail(strEmail) { var myReg = /^[-a-z0-9\._]+@([-a-z0-9\-]+\.)+[a-z0-9]{2,3}$/i; if (myReg.test(strEmail)) return true; return false; } //检测邮箱格式 function emailFormat(email) { return email.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/) ? true : false; } //检测手机格式 function phoneFormat(phone) { return phone.match(/^1(3|4|5|6|7|8)\d{9}$/) ? true : false; } //检测密码规则--6位以上 function pwdFormat(pwd) { return pwd.match(/^.*(?=.{6,})/) ? true : false; } //检测密码规则--6位以上数字字母组合 function pwdFormat6Mix(pwd) { return pwd.match(/^.*(?=.{5,})(?=.*\d)(?=.*[a-z]).*$/) ? true : false; } //必须字母开头,长度在6-18之间,只能包含字符(或下划线)、数字 function pwdFormat6MixNew(pwd) { return pwd.match(/^[a-zA-Z]\w{5,17}$/) ? true : false; } // 删除条目 function del(obj, id) { layer.confirm('确认要删除吗?', function (index) { $.post('delete', { 'id': id }, function (res) { if (res.code == 0) { $(obj).parents("tr").remove(); topalert({ type: 0, content: res.msg, speed: 1000 }); } else { topalert({ type: 2, content: res.msg, speed: 2000 }); } layer.close(layer.index); }, 'json'); }); } //列表多选删除条目 function del_all() { var checkbox = $('.text-c input[name="checkbox[]"]'); var ids = new Array(); checkbox.each(function (x) { if (this.checked) ids.push(this.value); }) var length = ids.length; if (length == 0) { layer.msg('请选择要删除的选项', { icon: 5, time: 1000 }); return false; } layer.confirm('确认要删除选中项吗?', function (index) { $.post('delete', { 'id': ids }, function (data) { if (data.code == 0) { layer.msg(data.msg, { icon: 1, time: 2000 }); window.location.reload(); } else { layer.msg(data.msg, { icon: 5, time: 2000 }); return false; } }, 'json'); }); } // 右上角提示框 function topalert(options, callback) { var defaults = { type: 0, content: '弹窗内容', speed: "0", }; var types = ['success', 'danger', 'error', 'info']; var options = $.extend(defaults, options); var length = $('.topalert').length; var htmlstr = '
' + options.content + '
'; if (options.speed == 0) { $(document.body).addClass("modal-open").append(htmlstr); $(".topalert" + length).fadeIn(); } else { $(document.body).addClass("modal-open").append(htmlstr); $(".topalert" + length).fadeIn(); setTimeout(function () { $(".topalert" + length).fadeOut("normal", complete => { $(".topalert" + length).remove(); }); }, options.speed); } if (callback) { callback(); } } $(function () { // 全选 $("input[name='allcheck']").change(function (e, x) { // console.log(this.checked); if (this.checked) { $('.text-c input[name="checkbox[]"]').prop("checked", true); } else { $('.text-c input[name="checkbox[]"]').prop("checked", false); } }) // 图片预览 $('.pictureview').hover(function () { console.log($(this).children('img').attr('src')); var url = $(this).children('img').attr('src'); var img = ''; $(this).append(img); }, function () { $('#thumbview').remove(); }); // 改变状态 $('.td-status .switch').on('switch-change', function () { var id = $(this).data('id'); var data = { 'id': parseInt(id) }; $.post('status', data, function (res) { if (res.code == 0) { topalert({ type: 0, content: res.msg, speed: 1000 }); } else { topalert({ type: 1, content: res.msg, speed: 1000 }); return false; } }, 'json'); }); // 排序 $(".input-sort").change(function () { var sort = $(this).val(); var id = $(this).data('id'); console.log(id); console.log(sort); $.post('sort', { 'id': id, 'sort': sort }, function (res) { if (res.code == 0) { topalert({ type: 0, content: res.msg, speed: 2000 }); } else { topalert({ type: 1, content: res.msg, speed: 2000 }); return false; } }, 'json'); }); });