fuxisheng/assets/js/common.js
2025-04-11 16:43:19 +08:00

153 lines
5.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

layui.use(['jquery', 'form', 'layer', 'element'], function () {
var $ = layui.$;
var form = layui.form;
var layer = layui.layer;
var element = layui.element;
/** 下拉框 必填验证 */
form.verify({
otherReq: function (value, item) {
var verifyName = $(item).attr('name')
, verifyType = $(item).attr('type')
, formElem = $(item).parents('.layui-form')//获取当前所在的form元素如果存在的话
, verifyElem = formElem.find('input[name=' + verifyName + ']')//获取需要校验的元素
, isTrue = verifyElem.is(':checked')//是否命中校验
, focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
if (!isTrue || !value) {
console.log(verifyName);
//定位焦点
focusElem.css(verifyType == 'radio' ? {"color": "#FF5722"} : {"border-color": "#FF5722"});
//对非输入框设置焦点
focusElem.first().attr("tabIndex", "1").css("outline", "0").blur(function () {
focusElem.css(verifyType == 'radio' ? {"color": ""} : {"border-color": ""});
}).focus();
return '必选项不能为空';
}
},
required: function (value, item) {
var verifyName = $(item).attr('name')
, formElem = $(item).parents('.layui-form')//获取当前所在的form元素如果存在的话
, verifyElem = formElem.find('input[name=' + verifyName + ']');//获取需要校验的元素
if (!value) {
console.log(verifyName);
//定位焦点
verifyElem.css({"border-color": "#FF5722"});
//对输入框设置焦点
verifyElem.first().attr("tabIndex", "1").css("outline", "0").blur(function () {
verifyElem.css({"border-color": ""});
}).focus();
return '必填项不能为空';
}
},
requiredSelect: function (value, item) {
var $ = layui.$;
var verifyName = $(item).attr('name')
, formElem = $(item).parents('.layui-form')//获取当前所在的form元素如果存在的话
, verifyElem = formElem.find('select[name=' + verifyName + ']');//获取需要校验的元素
if (!value) {
console.log(verifyName);
//定位焦点
verifyElem.css({"border-color": "#FF5722"});
//对下拉框设置焦点
formElem.find('select[name=' + verifyName + ']').first().next().find(".layui-input").attr("tabIndex", "1").css("outline", "0").blur(function () {
verifyElem.css({"border-color": ""});
}).focus();
return '必选项不能为空';
}
},
requiredTa: function (value, item) {
var verifyName = $(item).attr('name')
, formElem = $(item).parents('.layui-form')//获取当前所在的form元素如果存在的话
, verifyElem = formElem.find('textarea[name=' + verifyName + ']');//获取需要校验的元素
if (!value) {
console.log(verifyName);
//定位焦点
verifyElem.css({"border-color": "#FF5722"});
//对输入框设置焦点
verifyElem.first().attr("tabIndex", "1").css("outline", "0").blur(function () {
verifyElem.css({"border-color": ""});
}).focus();
return '必填项不能为空';
}
}
});
layer.photos({
photos: '.photo'
, anim: 5
});
});
/** 成功提示 */
function showOkMsg(msg) {
layer.msg(msg, {
icon: 1
});
}
/** 父页面成功提示 */
function parentShowOkMsg(msg) {
parent.layer.msg(msg, {
icon: 1
});
parent.layer.closeAll('iframe');
}
/** 失败提示 */
function showFailMsg(msg) {
layer.msg(msg, {
icon: 7,
anim: 6
});
}
/** 弹框提示 */
function showAlert(msg) {
layer.alert(msg, {title: '提示', icon: 7});
}
/** 弹框提示并跳转页面 */
function showAlertAndJump(msg, url) {
layer.alert(msg, {
closeBtn: 0,
icon: 1,
title: '提示'
}, function (index) {
jumpToUrl(url);
layer.close(index);
});
}
/** 网页跳转 */
function jumpToUrl(url) {
top.location = url;
}
/** 根据身份证号计算年龄 */
function getAgeByIdentity(identity) {
var len = (identity + "").length;
if (len == 0) {
return 0;
} else {
if ((len != 15) && (len != 18)){//身份证号码只能为15位或18位其它不合法
return 0;
}
}
var strBirthday = "";
if (len == 18){//处理18位的身份证号码从号码中得到生日和性别代码
strBirthday = identity.substr(6, 4) + "/" + identity.substr(10, 2) + "/" + identity.substr(12, 2);
}
if (len == 15) {
strBirthday = "19" + identity.substr(6, 2) + "/" + identity.substr(8, 2) + "/" + identity.substr(10, 2);
}
//时间字符串里,必须是“/”
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var age = nowDateTime.getFullYear() - birthDate.getFullYear();
//再考虑月、天的因素;.getMonth()获取的是从0开始的这里进行比较不需要加1
if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
age--;
}
return age;
}