为什么object.onkeyup=function(){}函数无法执行?

本文介绍了一个简单的密码强度检测程序,使用JavaScript实现。通过分析密码组成来评估强度,并通过不同颜色的提示展示结果。文章探讨了代码执行顺序对事件监听器的影响。

今天照着网上的《四级密码强度校验》视频写了一个相同的代码,主要作用就是在输入密码后js执行了一个方法,最终返回输入密码的强度等级。代码是这样的:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<style type="text/css">
			#tips{
				float:left;
				margin:2px 0px 0px 20px;
				color:#fff;
				overflow:hidden;
				background:#ccc;
			}
			span{
				float:left;
				width:50px;
				height:20px;
				text-align:center;
				line-height:20px;
				margin-right:2px;
			}
			#tips .s1{
				background:red;
			}
			#tips .s2{
				background:green;
			}
			#tips .s3{
				background:yellow;
			}
			#tips .s4{
				background:silver;
			}
		</style>
		<script type="text/javascript">
		
			var password=document.getElementById("pwd");

			password.onkeyup = function(){
				//var index = checkPassWord(this.value);
				alert(2);
			}
		
			
			function checkPassWord(value){
				var modes=0;
				if(value.length<6){
					
					return modes;
				}
				if(/\d/.test(value)){
					//如果密码包含了数字
					modes++;
				}
				if(/[a-z]/.test(value)){
					//如果密码包含了小写的英文字母a-z
					modes++;
				}
				if(/[A-Z]/.test(value)){
					//如果包含了大写的英文字母A-Z
					modes++;
				}
				if(/\W/.test(value)){
					//如果非数字,字母,下划线
					modes++;
				}
				
				return modes;
			}
		</script>
	</head>
	<body>
		<input type="text" id="pwd" maxlength="16" value=""/>
		<div id="tips">
			<span class="s1">弱</span>
			<span class="s2">中</span>
			<span class="s3">强</span>
			<span class="s4">很强</span>
		</div>
	</body>
	
</html>
以上代码处于未完成的调试状态。请注意script代码段的前四行代码,作用是找到id为pwd的元素,也就是那个input=text的元素,当在输入框输入字母后抬起键盘的那一刻,执行alert()函数,问题来了,一直都不会输出,也就是说如果onkeyup函数里的代码不执行的话这个密码校验功能也无法完成了。我看了看源码,发现作者把script代码块写在了<body>元素的后面,难道这有什么玄机?

结果我把代码也剪切倒了body元素后面,神奇的事情发生了,能成功执行 alert()方法了。。。

 我思考了下,可能是因为整个html文件加载的顺序是从上到下的,在加载到script文件时还没有password这个元素,所以他的oneyup函数自然也执行不了。但是我在var password=document.....这行代码下面加了alert(1);,发现可以正常打印,那为什么到password.onkeyup这里的代码缺执行不了呢。。

在script代码块位于input前的情况下,我加了一行window.onload=function(){},然后把原来前五行的代码剪切到function的方法体里,那么onkeyup函数能够正常执行。

暂时写到这里,这里遇到的问题先记录一下,最后附上这个校验密码强度的完整代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> 密码强度检测 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css">
	input{float: left; font-size: 14px; width: 250px; height: 25px; border: 1px solid #ccc; padding-left: 10px;}
	#tips{float: left; font-size: 12px; width: 400px; height: 25px; margin: 4px 0 0 20px;}
	#tips span{float: left; width: 40px; height: 20px; color: #fff; overflow:hidden; margin-right: 10px; background: #D7D9DD; line-height:20px; text-align: center; }
	#tips .s1{background: #F45A68;}/*红色*/
	#tips .s2{background: #fc0;}/*橙色*/
	#tips .s3{background: #cc0;}/*黄色*/
	#tips .s4{background: #14B12F;}/*绿色*/
  </style>
 </head>
 <body>
  <input type="text" id="password" value="" maxlength="16" />
  <div id="tips">
	<span>弱</span>
	<span>中</span>
	<span>强</span> 
	<span>很强</span>
  </div>
</body>
  <script type="text/javascript">
	var password = document.getElementById("password"); //获取文本框的对象
    //var value = password.value; //获取用户在文本框里面填写的值
	//获取所有的span标签 返回值是一个数组
	var spanDoms = document.getElementsByTagName("span");
	
	password.onkeyup = function(){
		//获取用户输入的密码,然后判断其强度 返回0 或者 1 2 3 4
		var index = checkPassWord(this.value);
		for(var i = 0; i <spanDoms.length; i++){
			spanDoms[i].className = "";//清空span标签所有的class样式
			if(index){//0 代表false 其余代表true
				spanDoms[index-1].className = "s" + index ;
			}
		}
	}
	
	//校验密码强度
	function checkPassWord(value){
		// 0: 表示第一个级别  1:表示第二个级别  2:表示第三个级别
		// 3: 表示第四个级别  4:表示第五个级别
        var modes = 0;		
		if(value.length < 6){//最初级别
			return modes;
		}
		
		if(/\d/.test(value)){//如果用户输入的密码 包含了数字
			modes++;
		}
		
		if(/[a-z]/.test(value)){//如果用户输入的密码 包含了小写的a到z
			modes++;
		}
		
		if(/[A-Z]/.test(value)){//如果用户输入的密码 包含了大写的A到Z
			modes++;
		}
		
		if(/\W/.test(value)){//如果是非数字 字母 下划线
			modes++;
		}
		
		switch(modes){
			case 1 : 
				return 1;
				break;
			case 2 :
				return 2;
				break;
			case 3 :
				return 3;
				break;
			case 4 :
				return 4;
				break;
		}
		
	}
  </script>
</html>


// JScript 檔 // 主调用函数是 setday(this,[object])和setday(this),[object]是控件输出的控件名,举两个例子: // 一、<input name=txt><input type=button value=setday onclick="setday(this,document.all.txt)"> // 二、<input onfocus="setday(this)"> var bMoveable = true; var strFrame; document.writeln('<iframe id=endDateLayer frameborder=0 width=162 height=211 style="position: absolute; z-index: 9998; display: none"></iframe>'); strFrame = '<style>'; strFrame += 'INPUT.button{BORDER-RIGHT: #63A3E9 1px solid;BORDER-TOP: #63A3E9 1px solid;BORDER-LEFT: #63A3E9 1px solid;'; strFrame += 'BORDER-BOTTOM: #63A3E9 1px solid;BACKGROUND-COLOR: #63A3E9;font-family:宋体;}'; strFrame += 'TD{FONT-SIZE: 9pt;font-family:宋体;}'; strFrame += '</style>'; strFrame += '<scr' + 'ipt>'; strFrame += 'var datelayerx,datelayery;'; strFrame += 'var bDrag;'; strFrame += 'function document.onmousemove()'; strFrame += '{if(bDrag && window.event.button==1)'; strFrame += ' {var DateLayer=parent.document.all.endDateLayer.style;'; strFrame += ' DateLayer.posLeft += window.event.clientX-datelayerx;'; strFrame += ' DateLayer.posTop += window.event.clientY-datelayery;}}'; strFrame += 'function DragStart()'; strFrame += '{var DateLayer=parent.document.all.endDateLayer.style;'; strFrame += ' datelayerx=window.event.clientX;'; strFrame += ' datelayery=window.event.clientY;'; strFrame += ' bDrag=true;}'; strFrame += 'function DragEnd(){'; strFrame += ' bDrag=false;}'; strFrame += '</scr' + 'ipt>'; strFrame += '<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false">'; strFrame += '<span id=tmpSelectYearLayer style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>'; strFrame += '<span id=tmpSelectMonthLayer style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>'; strFrame += '<span id=tmpSelectHourLayer style="z-index: 9999;position: absolute;top: 188; left: 35px;display: none"></span>'; strFrame += '<span id=tmpSelectMinuteLayer style="z-index:9999;position:absolute;top: 188; left: 77px;display: none"></span>'; strFrame += '<span id=tmpSelectSecondLayer style="z-index:9999;position:absolute;top: 188; left: 119px;display: none"></span>'; strFrame += '<table border=1 cellspacing=0 cellpadding=0 width=142 height=160 bordercolor=#63A3E9 bgcolor=#63A3E9 >'; strFrame += ' <tr><td width=142 height=23 bgcolor=#FFFFFF>'; strFrame += ' <table border=0 cellspacing=1 cellpadding=0 width=158 height=23>'; strFrame += ' <tr align=center >'; strFrame += ' <td width=16 align=center bgcolor=#63A3E9 style="font-size:12px;cursor: hand;color: #ffffff"; display: none'; strFrame += ' onclick="parent.meizzPrevM()" title="向前翻 1 月" ><b ><</b></td>'; strFrame += ' <td width=60 align="center" bgcolor="#63A3E9" style="font-size:12px;cursor:hand" '; strFrame += ' onmouseover="style.backgroundColor=\'#aaccf3\'"'; strFrame += ' onmouseout="style.backgroundColor=\'#63A3E9\'" '; strFrame += ' onclick="parent.tmpSelectYearInnerHTML(this.innerText.substring(0,4))" '; strFrame += ' title="點擊這裡選擇年份"><span id=meizzYearHead></span></td>'; strFrame += ' <td width=48 align="center" style="font-size:12px;font-color: #ffffff;cursor:hand" '; strFrame += ' bgcolor="#63A3E9" onmouseover="style.backgroundColor=\'#aaccf3\'" '; strFrame += ' onmouseout="style.backgroundColor=\'#63A3E9\'" '; strFrame += ' onclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"'; strFrame += ' title="點擊這裡選擇月份"><span id=meizzMonthHead ></span></td>'; strFrame += ' <td width=16 bgcolor=#63A3E9 align=center style="font-size:12px;cursor: hand;color: #ffffff" '; strFrame += ' onclick="parent.meizzNextM()" title="向后翻 1 月" ><b >></b></td>'; strFrame += ' </tr>'; strFrame += ' </table></td></tr>'; strFrame += ' <tr><td width=142 height=18 >'; strFrame += ' <table border=0 cellspacing=0 cellpadding=2 bgcolor=#63A3E9 ' + (bMoveable ? 'onmousedown="DragStart()" onmouseup="DragEnd()"' : ''); strFrame += ' BORDERCOLORLIGHT=#63A3E9 BORDERCOLORDARK=#FFFFFF width=140 height=20 style="cursor:' + (bMoveable ? 'move' : 'default') + '">'; strFrame += ' <tr><td style="font-size:12px;color:#ffffff" width=20> 日</td>'; strFrame += '<td style="font-size:12px;color:#FFFFFF" > 一</td><td style="font-size:12px;color:#FFFFFF"> 二</td>'; strFrame += '<td style="font-size:12px;color:#FFFFFF" > 三</td><td style="font-size:12px;color:#FFFFFF" > 四</td>'; strFrame += '<td style="font-size:12px;color:#FFFFFF" > 五</td><td style="font-size:12px;color:#FFFFFF" > 六</td></tr>'; strFrame += '</table></td></tr>'; strFrame += ' <tr ><td width=142 height=120 >'; strFrame += ' <table border=1 cellspacing=2 cellpadding=2 BORDERCOLORLIGHT=#63A3E9 BORDERCOLORDARK=#FFFFFF bgcolor=#fff8ec width=140 height=120 >'; var n = 0; for (j = 0; j < 5; j++) { strFrame += ' <tr align=center >'; for (i = 0; i < 7; i++) { strFrame += '<td width=20 height=20 id=meizzDay' + n + ' style="font-size:12px" onclick=parent.meizzDayClick(this.innerText,0)></td>'; n++; } strFrame += '</tr>'; } strFrame += ' <tr align=center >'; for (i = 35; i < 37; i++) strFrame += '<td width=20 height=20 id=meizzDay' + i + ' style="font-size:12px" onclick="parent.meizzDayClick(this.innerText,0)"></td>'; strFrame += ' <td colspan=5 align=right style="color:#1478eb"><span onclick="parent.setNull()" style="font-size:12px;cursor: hand"'; strFrame += ' onmouseover="style.color=\'#ff0000\'" onmouseout="style.color=\'#1478eb\'" title="将日期置空">置空</span> <span onclick="parent.meizzToday()" style="font-size:12px;cursor: hand"'; strFrame += ' onmouseover="style.color=\'#ff0000\'" onmouseout="style.color=\'#1478eb\'" title="当前日期时间">當前</span> <span style="cursor:hand" id=evaAllOK onmouseover="style.color=\'#ff0000\'" onmouseout="style.color=\'#1478eb\'" onclick="parent.closeLayer()" title="關閉日曆">確定 </span></td></tr>'; strFrame += ' </table></td></tr><tr visible="false"><td >'; strFrame += ' <table border=0 cellspacing=1 cellpadding=0 width=100% bgcolor=#FFFFFF height=22 >'; strFrame += ' <tr bgcolor="#63A3E9"><td id=bUseTimeLayer width=30 style="cursor:hand" title="點擊這裡啟用/禁用時間"'; strFrame += ' onmouseover="style.backgroundColor=\'#aaccf3\'" align=center onmouseout="style.backgroundColor=\'#63A3E9\'"'; strFrame += ' onclick="parent.UseTime(this)">'; strFrame += ' <span></span></td>'; strFrame += ' <td style="cursor:hand" onclick="parent.tmpSelectHourInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"'; strFrame += ' onmouseover="style.backgroundColor=\'#aaccf3\'" onmouseout="style.backgroundColor=\'#63A3E9\'"'; strFrame += ' title="點擊這裡選擇時間" align=center width=42>'; strFrame += ' <span id=meizzHourHead></span></td>'; strFrame += ' <td style="cursor:hand" onclick="parent.tmpSelectMinuteInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"'; strFrame += ' onmouseover="style.backgroundColor=\'#aaccf3\'" onmouseout="style.backgroundColor=\'#63A3E9\'"'; strFrame += ' title="點擊這裡選擇時間" align=center width=42>'; strFrame += ' <span id=meizzMinuteHead></span></td>'; strFrame += ' <td style="cursor:hand" onclick="parent.tmpSelectSecondInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"'; strFrame += ' onmouseover="style.backgroundColor=\'#aaccf3\'" onmouseout="style.backgroundColor=\'#63A3E9\'"'; strFrame += ' title="點擊這裡選擇時間" align=center width=42>'; strFrame += ' <span id=meizzSecondHead></span></td>'; strFrame += ' </tr></table></td></tr></table></div>'; window.frames.endDateLayer.document.writeln(strFrame); window.frames.endDateLayer.document.close(); //解决ie进度条不结束的问题 //==================================================== WEB 页面显示部分 ====================================================== var outObject; var outButton; //点击的按钮 var outDate = ""; //存放对象的日期 var bUseTime = false; //是否使用时间 var odatelayer = window.frames.endDateLayer.document.all; //存放日历对象 var odatelayer = window.endDateLayer.document.all; //odatelayer.bUseTimeLayer.innerText="NO"; bImgSwitch(); odatelayer.bUseTimeLayer.innerHTML = bImg; function setday(tt, obj) //主调函数 { if (arguments.length > 2) { alert("对不起!传入本控件的参数太多!"); return; } if (arguments.length == 0) { alert("对不起!您没有传回本控件任何参数!"); return; } var dads = document.all.endDateLayer.style; var th = tt; var ttop = tt.offsetTop; //TT控件的定位点高 var thei = tt.clientHeight; //TT控件本身的高 var tleft = tt.offsetLeft; //TT控件的定位点宽 var ttyp = tt.type; //TT控件的类型 while (tt = tt.offsetParent) { ttop += tt.offsetTop; tleft += tt.offsetLeft; } dads.top = (ttyp == "image") ? ttop + thei : ttop + thei + 6; dads.left = tleft; outObject = (arguments.length == 1) ? th : obj; outButton = (arguments.length == 1) ? null : th; //设定外部点击的按钮 //根据当前输入框的日期显示日历的年月 var reg = /^(\d+)-(\d{1,2})-(\d{1,2})/; //不含时间 var r = outObject.value.match(reg); if (r != null) { r[2] = r[2] - 1; var d = new Date(r[1], r[2], r[3]); if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3]) { outDate = d; parent.meizzTheYear = r[1]; parent.meizzTheMonth = r[2]; parent.meizzTheDate = r[3]; } else { outDate = ""; } meizzSetDay(r[1], r[2] + 1); } else { outDate = ""; meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1); } dads.display = ''; //判断初始化时是否使用时间,非严格验证 //if (outObject.value.length>10) //{ bUseTime = true; bImgSwitch(); odatelayer.bUseTimeLayer.innerHTML = bImg; meizzWriteHead(meizzTheYear, meizzTheMonth); //} //else //{ // bUseTime=false; // bImgSwitch(); // odatelayer.bUseTimeLayer.innerHTML=bImg; // meizzWriteHead(meizzTheYear,meizzTheMonth); //} try { event.returnValue = false; } catch (e) { //此处排除错误,错误原因暂未找到。 } } var MonHead = new Array(12); //定义阳历中每个月的最大天数 MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4] = 31; MonHead[5] = 30; MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31; var meizzTheYear = new Date().getFullYear(); //定义年的变量的初始值 var meizzTheMonth = new Date().getMonth() + 1; //定义月的变量的初始值 var meizzTheDate = new Date().getDate(); //定义日的变量的初始值 var meizzTheHour = new Date().getHours(); //定义小时变量的初始值 var meizzTheMinute = new Date().getMinutes(); //定义分钟变量的初始值 var meizzTheSecond = new Date().getSeconds(); //定义秒变量的初始值 var meizzWDay = new Array(37); //定义写日期的数组 function document.onclick() //任意点击时关闭该控件 //ie6的情况可以由下面的切换焦点处理代替 { with (window.event) { if (srcElement != outObject && srcElement != outButton) closeLayer(); } } function document.onkeyup() //按Esc键关闭,切换焦点关闭 { if (window.event.keyCode == 27) { if (outObject) outObject.blur(); closeLayer(); } else if (document.activeElement) { if (document.activeElement != outObject && document.activeElement != outButton) { closeLayer(); } } } function meizzWriteHead(yy, mm, ss) //往 head 中写入当前的年与月 { odatelayer.meizzYearHead.innerText = yy + " 年"; odatelayer.meizzMonthHead.innerText = format(mm) + " 月"; //插入当前小时、分 odatelayer.meizzHourHead.innerText = bUseTime ? (meizzTheHour + " 时") : ""; odatelayer.meizzMinuteHead.innerText = bUseTime ? (meizzTheMinute + " 分") : ""; odatelayer.meizzSecondHead.innerText = bUseTime ? (meizzTheSecond + " 秒") : ""; } function tmpSelectYearInnerHTML(strYear) //年份的下拉框 { if (strYear.match(/\D/) != null) { alert("年份输入参数不是数字!"); return; } var m = (strYear) ? strYear : new Date().getFullYear(); if (m < 1000 || m > 9999) { alert("年份值不在 1000 到 9999 之间!"); return; } var n = m - 50; if (n < 1000) n = 1000; if (n + 101 > 9999) n = 9974; var s = " <select name=tmpSelectYear style='font-size: 12px' " s += "onblur='document.all.tmpSelectYearLayer.style.display=\"none\"' " s += "onchange='document.all.tmpSelectYearLayer.style.display=\"none\";" s += "parent.meizzTheYear = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n"; var selectInnerHTML = s; for (var i = n; i < n + 101; i++) { if (i == m) { selectInnerHTML += "<option value='" + i + "' selected>" + i + "年" + "</option>\r\n"; } else { selectInnerHTML += "<option value='" + i + "'>" + i + "年" + "</option>\r\n"; } } selectInnerHTML += "</select>"; odatelayer.tmpSelectYearLayer.style.display = ""; odatelayer.tmpSelectYearLayer.innerHTML = selectInnerHTML; odatelayer.tmpSelectYear.focus(); } function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框 { if (strMonth.match(/\D/) != null) { alert("月份输入参数不是数字!"); return; } var m = (strMonth) ? strMonth : new Date().getMonth() + 1; var s = " <select name=tmpSelectMonth style='font-size: 12px' " s += "onblur='document.all.tmpSelectMonthLayer.style.display=\"none\"' " s += "onchange='document.all.tmpSelectMonthLayer.style.display=\"none\";" s += "parent.meizzTheMonth = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n"; var selectInnerHTML = s; for (var i = 1; i < 13; i++) { if (i == m) { selectInnerHTML += "<option value='" + i + "' selected>" + i + "月" + "</option>\r\n"; } else { selectInnerHTML += "<option value='" + i + "'>" + i + "月" + "</option>\r\n"; } } selectInnerHTML += "</select>"; odatelayer.tmpSelectMonthLayer.style.display = ""; odatelayer.tmpSelectMonthLayer.innerHTML = selectInnerHTML; odatelayer.tmpSelectMonth.focus(); } /**//***** 增加 小时、分钟 ***/ function tmpSelectHourInnerHTML(strHour) //小时的下拉框 { if (!bUseTime) { return; } if (strHour.match(/\D/) != null) { alert("小时输入参数不是数字!"); return; } var m = (strHour) ? strHour : new Date().getHours(); var s = "<select name=tmpSelectHour style='font-size: 12px' " s += "onblur='document.all.tmpSelectHourLayer.style.display=\"none\"' " s += "onchange='document.all.tmpSelectHourLayer.style.display=\"none\";" s += "parent.meizzTheHour = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute);'>\r\n"; var selectInnerHTML = s; for (var i = 0; i < 24; i++) { if (i == m) { selectInnerHTML += "<option value='" + i + "' selected>" + i + "</option>\r\n"; } else { selectInnerHTML += "<option value='" + i + "'>" + i + "</option>\r\n"; } } selectInnerHTML += "</select>"; odatelayer.tmpSelectHourLayer.style.display = ""; odatelayer.tmpSelectHourLayer.innerHTML = selectInnerHTML; odatelayer.tmpSelectHour.focus(); } function tmpSelectMinuteInnerHTML(strMinute) //分钟的下拉框 { if (!bUseTime) { return; } if (strMinute.match(/\D/) != null) { alert("分钟输入参数不是数字!"); return; } var m = (strMinute) ? strMinute : new Date().getMinutes(); var s = "<select name=tmpSelectMinute style='font-size: 12px' " s += "onblur='document.all.tmpSelectMinuteLayer.style.display=\"none\"' " s += "onchange='document.all.tmpSelectMinuteLayer.style.display=\"none\";" s += "parent.meizzTheMinute = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute);'>\r\n"; var selectInnerHTML = s; for (var i = 0; i < 60; i++) { // if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"</option>\r\n"; } // else { selectInnerHTML += "<option value='"+i+"'>"+i+"</option>\r\n"; } if (i == m) { if (i < 10) selectInnerHTML += "<option value='0" + i + "' selected>0" + i + "</option>\r\n"; else selectInnerHTML += "<option value='" + i + "' selected>" + i + "</option>\r\n"; } else { if (i < 10) selectInnerHTML += "<option value='0" + i + "'>0" + i + "</option>\r\n"; else selectInnerHTML += "<option value='" + i + "'>" + i + "</option>\r\n"; } } selectInnerHTML += "</select>"; odatelayer.tmpSelectMinuteLayer.style.display = ""; odatelayer.tmpSelectMinuteLayer.innerHTML = selectInnerHTML; odatelayer.tmpSelectMinute.focus(); } function tmpSelectSecondInnerHTML(strSecond) //秒的下拉框 { if (!bUseTime) { return; } if (strSecond.match(/\D/) != null) { alert("分钟输入参数不是数字!"); return; } var m = (strSecond) ? strSecond : new Date().getMinutes(); var s = "<select name=tmpSelectSecond style='font-size: 12px' " s += "onblur='document.all.tmpSelectSecondLayer.style.display=\"none\"' " s += "onchange='document.all.tmpSelectSecondLayer.style.display=\"none\";" s += "parent.meizzTheSecond = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute,parent.meizzTheSecond);'>\r\n"; var selectInnerHTML = s; for (var i = 0; i < 60; i++) { // if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"</option>\r\n"; } // else { selectInnerHTML += "<option value='"+i+"'>"+i+"</option>\r\n"; } if (i == m) { if (i < 10) selectInnerHTML += "<option value='0" + i + "' selected>0" + i + "</option>\r\n"; else selectInnerHTML += "<option value='" + i + "' selected>" + i + "</option>\r\n"; } else { if (i < 10) selectInnerHTML += "<option value='0" + i + "'>0" + i + "</option>\r\n"; else selectInnerHTML += "<option value='" + i + "'>" + i + "</option>\r\n"; } } selectInnerHTML += "</select>"; odatelayer.tmpSelectSecondLayer.style.display = ""; odatelayer.tmpSelectSecondLayer.innerHTML = selectInnerHTML; odatelayer.tmpSelectSecond.focus(); } function closeLayer() //这个层的关闭 { var o = document.getElementById("endDateLayer"); if (o != null) { o.style.display = "none"; } } function showLayer() //这个层的关闭 { document.all.endDateLayer.style.display = ""; } function IsPinYear(year) //判断是否闰平年 { if (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0))) return true; else return false; } function GetMonthCount(year, month) //闰年二月为29天 { var c = MonHead[month - 1]; if ((month == 2) && IsPinYear(year)) c++; return c; } function GetDOW(day, month, year) //求某天的星期几 { var dt = new Date(year, month - 1, day).getDay() / 7; return dt; } function meizzPrevY() //往前翻 Year { if (meizzTheYear > 999 && meizzTheYear < 10000) { meizzTheYear--; } else { alert("年份超出范围(1000-9999)!"); } meizzSetDay(meizzTheYear, meizzTheMonth); } function meizzNextY() //往后翻 Year { if (meizzTheYear > 999 && meizzTheYear < 10000) { meizzTheYear++; } else { alert("年份超出范围(1000-9999)!"); } meizzSetDay(meizzTheYear, meizzTheMonth); } function setNull() { outObject.value = ''; closeLayer(); } function meizzToday() //Today Button { parent.meizzTheYear = new Date().getFullYear(); parent.meizzTheMonth = new Date().getMonth() + 1; parent.meizzTheDate = new Date().getDate(); parent.meizzTheHour = new Date().getHours(); parent.meizzTheMinute = new Date().getMinutes(); parent.meizzTheSecond = new Date().getSeconds(); var meizzTheSecond = new Date().getSeconds(); if (meizzTheMonth < 10 && meizzTheMonth.length < 2) //格式化成两位数字 { parent.meizzTheMonth = "0" + parent.meizzTheMonth; } if (parent.meizzTheDate < 10 && parent.meizzTheDate.length < 2) //格式化成两位数字 { parent.meizzTheDate = "0" + parent.meizzTheDate; } //meizzSetDay(meizzTheYear,meizzTheMonth); if (outObject) { if (bUseTime) { outObject.value = parent.meizzTheYear + "/" + format(parent.meizzTheMonth) + "/" + format(parent.meizzTheDate) + " " + format(parent.meizzTheHour) + ":" + format(parent.meizzTheMinute) //+ ":" + format(parent.meizzTheSecond); //注:在这里你可以输出改成你想要的格式 } else { outObject.value = parent.meizzTheYear + "-" + format(parent.meizzTheMonth) + "-" + format(parent.meizzTheDate); //注:在这里你可以输出改成你想要的格式 } } closeLayer(); } function meizzPrevM() //往前翻月份 { if (meizzTheMonth > 1) { meizzTheMonth-- } else { meizzTheYear--; meizzTheMonth = 12; } meizzSetDay(meizzTheYear, meizzTheMonth); } function meizzNextM() //往后翻月份 { if (meizzTheMonth == 12) { meizzTheYear++; meizzTheMonth = 1 } else { meizzTheMonth++ } meizzSetDay(meizzTheYear, meizzTheMonth); } // TODO: 整理代码 function meizzSetDay(yy, mm) //主要的写程序********** { meizzWriteHead(yy, mm); //设置当前年月的公共变量为传入值 meizzTheYear = yy; meizzTheMonth = mm; for (var i = 0; i < 37; i++) { meizzWDay[i] = "" }; //将显示框的内容全部清空 var day1 = 1, day2 = 1, firstday = new Date(yy, mm - 1, 1).getDay(); //某月第一天的星期几 for (i = 0; i < firstday; i++) meizzWDay[i] = GetMonthCount(mm == 1 ? yy - 1 : yy, mm == 1 ? 12 : mm - 1) - firstday + i + 1 //上个月的最后几天 for (i = firstday; day1 < GetMonthCount(yy, mm) + 1; i++) { meizzWDay[i] = day1; day1++; } for (i = firstday + GetMonthCount(yy, mm); i < 37; i++) { meizzWDay[i] = day2; day2++; } for (i = 0; i < 37; i++) { var da = eval("odatelayer.meizzDay" + i) //书写新的一个月的日期星期排列 if (meizzWDay[i] != "") { //初始化边框 da.borderColorLight = "#63A3E9"; da.borderColorDark = "#63A3E9"; da.style.color = "#1478eb"; if (i < firstday) //上个月的部分 { da.innerHTML = "<b><font color=#BCBABC>" + meizzWDay[i] + "</font></b>"; da.title = (mm == 1 ? 12 : mm - 1) + "月" + meizzWDay[i] + "日"; da.onclick = Function("meizzDayClick(this.innerText,-1)"); if (!outDate) da.style.backgroundColor = ((mm == 1 ? yy - 1 : yy) == new Date().getFullYear() && (mm == 1 ? 12 : mm - 1) == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"; else { da.style.backgroundColor = ((mm == 1 ? yy - 1 : yy) == outDate.getFullYear() && (mm == 1 ? 12 : mm - 1) == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) ? "#84C1FF" : (((mm == 1 ? yy - 1 : yy) == new Date().getFullYear() && (mm == 1 ? 12 : mm - 1) == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"); //将选中的日期显示为凹下去 if ((mm == 1 ? yy - 1 : yy) == outDate.getFullYear() && (mm == 1 ? 12 : mm - 1) == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) { da.borderColorLight = "#FFFFFF"; da.borderColorDark = "#63A3E9"; } } } else if (i >= firstday + GetMonthCount(yy, mm)) //下个月的部分 { da.innerHTML = "<b><font color=#BCBABC>" + meizzWDay[i] + "</font></b>"; da.title = (mm == 12 ? 1 : mm + 1) + "月" + meizzWDay[i] + "日"; da.onclick = Function("meizzDayClick(this.innerText,1)"); if (!outDate) da.style.backgroundColor = ((mm == 12 ? yy + 1 : yy) == new Date().getFullYear() && (mm == 12 ? 1 : mm + 1) == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"; else { da.style.backgroundColor = ((mm == 12 ? yy + 1 : yy) == outDate.getFullYear() && (mm == 12 ? 1 : mm + 1) == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) ? "#84C1FF" : (((mm == 12 ? yy + 1 : yy) == new Date().getFullYear() && (mm == 12 ? 1 : mm + 1) == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"); //将选中的日期显示为凹下去 if ((mm == 12 ? yy + 1 : yy) == outDate.getFullYear() && (mm == 12 ? 1 : mm + 1) == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) { da.borderColorLight = "#FFFFFF"; da.borderColorDark = "#63A3E9"; } } } else //本月的部分 { da.innerHTML = "<b>" + meizzWDay[i] + "</b>"; da.title = mm + "月" + meizzWDay[i] + "日"; da.onclick = Function("meizzDayClick(this.innerText,0)"); //给td赋予onclick事件的处理 //如果是当前选择的日期,则显示亮蓝色的背景;如果是当前日期,则显示暗黄色背景 if (!outDate) da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"; else { da.style.backgroundColor = (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) ? "#84C1FF" : ((yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && meizzWDay[i] == new Date().getDate()) ? "#5CEFA0" : "#f5f5f5"); //将选中的日期显示为凹下去 if (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && meizzWDay[i] == outDate.getDate()) { da.borderColorLight = "#FFFFFF"; da.borderColorDark = "#63A3E9"; } } } da.style.cursor = "hand" } else { da.innerHTML = ""; da.style.backgroundColor = ""; da.style.cursor = "default"; } } } function meizzDayClick(n, ex) //点击显示框选取日期,主输入函数************* { parent.meizzTheDate = n; var yy = meizzTheYear; var mm = parseInt(meizzTheMonth) + ex; //ex表示偏移量,用于选择上个月份和下个月份的日期 var hh = meizzTheHour; var mi = meizzTheMinute; var se = meizzTheSecond; //判断月份,并进行对应的处理 if (mm < 1) { yy--; mm = 12 + mm; } else if (mm > 12) { yy++; mm = mm - 12; } if (mm < 10) { mm = "0" + mm; } if (hh < 10) { hh = "0" + hh; } //时 // if (mi<10) {mi="0" + mi;} //分 if (mi < 10) { mi = "" + mi; } //分 if (se < 10) { se = "0" + se; } //秒 if (outObject) { if (!n) { //outObject.value=""; return; } if (n < 10) { n = "0" + n; } WriteDateTo(yy, mm, n, hh, mi, se); closeLayer(); if (bUseTime) { try { outButton.click(); } catch (e) { setday(outObject); } } } else { closeLayer(); alert("您所要输出的控件对象并不存在!"); } } function format(n) //格式化数字为两位字符表示 { var m = new String(); var tmp = new String(n); if (n < 10 && tmp.length < 2) { m = "0" + n; } else { m = n; } return m; } function evaSetTime() //设置用户选择的小时、分钟 { odatelayer.meizzHourHead.innerText = meizzTheHour + " 时"; odatelayer.meizzMinuteHead.innerText = meizzTheMinute + " 分"; odatelayer.meizzSecondHead.innerText = meizzTheSecond + " 秒"; WriteDateTo(meizzTheYear, meizzTheMonth, meizzTheDate, meizzTheHour, meizzTheMinute, meizzTheSecond) } function evaSetTimeNothing() //设置时间控件为空 { odatelayer.meizzHourHead.innerText = ""; odatelayer.meizzMinuteHead.innerText = ""; odatelayer.meizzSecondHead.innerText = ""; WriteDateTo(meizzTheYear, meizzTheMonth, meizzTheDate, meizzTheHour, meizzTheMinute, meizzTheSecond) } function evaSetTimeNow() //设置时间控件为当前时间 { odatelayer.meizzHourHead.innerText = new Date().getHours() + " 时"; odatelayer.meizzMinuteHead.innerText = new Date().getMinutes() + " 分"; odatelayer.meizzSecondHead.innerText = new Date().getSeconds() + " 秒"; meizzTheHour = new Date().getHours(); meizzTheMinute = new Date().getMinutes(); meizzTheSecond = new Date().getSeconds(); WriteDateTo(meizzTheYear, meizzTheMonth, meizzTheDate, meizzTheHour, meizzTheMinute, meizzTheSecond) } function UseTime(ctl) { bUseTime = !bUseTime; if (bUseTime) { bImgSwitch(); ctl.innerHTML = bImg; evaSetTime(); //显示时间,用户原来选择的时间 //evaSetTimeNow(); //显示当前时间 } else { bImgSwitch(); ctl.innerHTML = bImg; evaSetTimeNothing(); } } function WriteDateTo(yy, mm, n, hh, mi, se) { if (bUseTime) { //outObject.value= yy + "/" + format(mm) + "/" + format(n) + " " + format(hh) + ":" + format(mi) + ":" + format(se); //注:在这里你可以输出改成你想要的格式 outObject.value = yy + "/" + format(mm) + "/" + format(n) + " " + format(hh) + ":" + format(mi); } else { //outObject.value= format(mm) + "/" + format(n)+"/"+yy; outObject.value = yy + "/" + format(mm) + "/" + format(n); //注:在这里你可以输出改成你想要的格式 } } function bImgSwitch() { if (bUseTime) { bImg = "開啟"; } else { bImg = "確定"; } } 这是日期控件的内容
09-11
<!-- 盘点新增/Add --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="../../layui/css/layui.css"> <link rel="stylesheet" href="../../css/diy.css"> <script src="../../js/axios.min.js"></script> <style> img { width: 200px; } .layui-upload-list{ overflow: hidden; } .layui-upload-list .multiple_block .upload_img_multiple{ height: auto; width: 100px; } .multiple_block{ position: relative; float: left; width: 100px; margin: 0 10px 10px 0; } .multiple_block .upload-img-del{ position: absolute; top: 5px; right: 5px; color: #fff; border-radius: 100%; background: #0000009c; width: 20px; height: 20px; text-align: center; line-height: 20px; cursor: pointer; } </style> </head> <body> <article class="sign_in"> <div class="warp tpl"> <div class="layui-container"> <div class="layui-row"> <form class="layui-form" action=""> <div class="form-input-box from-input-box-i"> <div class="layui-form-item unique" id="warehouse_name_box"> <label class="layui-form-label">仓库名称</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入仓库名称" class="layui-input" id="warehouse_name"> </div> </div> <div class="layui-form-item unique" id="goods_number_box"> <label class="layui-form-label">货物编号</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入货物编号" class="layui-input" id="goods_number"> </div> </div> <div class="layui-form-item unique" id="name_of_goods_box"> <label class="layui-form-label">货物名称</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入货物名称" class="layui-input" id="name_of_goods"> </div> </div> <div class="layui-form-item unique" id="classification_of_goods_box"> <label class="layui-form-label">货物分类</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入货物分类" class="layui-input" id="classification_of_goods"> </div> </div> <div class="layui-form-item number" id="inventory_of_goods_box"> <label class="layui-form-label">货物库存</label> <div class="layui-input-block input-i block"> <input type="number" name="num" class="layui-input num" id="inventory_of_goods"> </div> </div> <div class="layui-form-item unique" id="storage_location_box"> <label class="layui-form-label">存放位置</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入存放位置" class="layui-input" id="storage_location"> </div> </div> <div class="layui-form-item uid-box" id="employee_users_box"> <label class="layui-form-label">员工</label> <div class="layui-input-block select block"> <select name="interest" lay-filter="employee_users" id="employee_users"> <option value=""></option> </select> </div> </div> <div class="layui-form-item unique" id="employee_name_box"> <label class="layui-form-label">员工姓名</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入员工姓名" class="layui-input" id="employee_name"> </div> </div> <div class="layui-form-item unique" id="employee_id_box"> <label class="layui-form-label">员工工号</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入员工工号" class="layui-input" id="employee_id"> </div> </div> <div class="layui-form-item tiem-box" id="inventory_date_box"> <div class="layui-inline"> <label class="layui-form-label">盘点日期</label> <div class="layui-input-inline"> <input type="text" class="layui-input" id="inventory_date" placeholder=""> </div> </div> </div> <div class="layui-form-item number" id="physical_inventory_box"> <label class="layui-form-label">盘点数量</label> <div class="layui-input-block input-i block"> <input type="number" name="num" class="layui-input num" id="physical_inventory"> </div> </div> <div class="layui-form-item layui-form-text" id="inventory_notes_box"> <label class="layui-form-label">盘点备注</label> <div class="layui-input-block text"> <textarea placeholder="请输入盘点备注" class="layui-textarea" id="inventory_notes"></textarea> </div> </div> </div> </form> <div class="layui-btn-container"> <button type="button" class="layui-btn layui-btn-normal login" id="submit">确认</button> <button type="button" class="layui-btn layui-btn-normal login" id="cancel">取消</button> </div> </div> </div> </div> </article> </body> <script src="../../layui/layui.js"></script> <script src="../../js/base.js"></script> <script src="../../js/index.js"></script> <script> var BaseUrl = baseUrl() let cancel = document.querySelector("#cancel") cancel.addEventListener("click",()=>{ colseLayer() }) let inventory_records_id = location.search.substring(1) layui.use(['upload', 'element', 'layer', 'laydate', 'layedit'], function () { var $ = layui.jquery , upload = layui.upload , element = layui.element , layer = layui.layer , laydate = layui.laydate , layedit = layui.layedit , form = layui.form; let url let token = sessionStorage.token || null let personInfo = JSON.parse(sessionStorage.personInfo) let user_group = personInfo.user_group let use_id = personInfo.user_id function $get_stamp() { return new Date().getTime(); } function $get_rand(len) { var rand = Math.random(); return Math.ceil(rand * 10 ** len); } // 权限判断 /** * 获取路径对应操作权限 鉴权 * @param {String} action 操作名 */ function $check_action(path1, action = "get") { var o = $get_power(path1); if (o && o[action] != 0 && o[action] != false) { return true; } return false; } /** * 是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 */ function $check_field(action, field, path1) { var o = $get_power(path1); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 获取权限 * @param {String} path 路由路径 */ function $get_power(path) { //从sessionStorage读取权限配置,验证用户操作权限 var list_data = JSON.parse(sessionStorage.list_data) var list = list_data; var obj; for (var i = 0; i < list.length; i++) { var o = list[i]; if (o.path === path) { obj = o; break; } } return obj; } let submit = document.querySelector('#submit') // 提交按钮校验权限 if ($check_action('/inventory_records/view', 'add') || $check_action('/inventory_records/view', 'set') || $check_option('/inventory_records/table', 'examine')) { }else { $("#submit").hide() } // style="display: none" let field = "inventory_records_id"; let url_add = "inventory_records"; let url_set = "inventory_records"; let url_get_obj = "inventory_records"; let url_upload = "inventory_records" let query = { "inventory_records_id": 0, } let form_data2 = { "warehouse_name": '', // 仓库名称 "goods_number": '', // 货物编号 "name_of_goods": '', // 货物名称 "classification_of_goods": '', // 货物分类 "inventory_of_goods": 0, // 货物库存 "storage_location": '', // 存放位置 "employee_users": 0, // 员工 "employee_name": '', // 员工姓名 "employee_id": '', // 员工工号 "inventory_date": '', // 盘点日期 "physical_inventory": 0, // 盘点数量 "inventory_notes": '', // 盘点备注 "inventory_records_id": 0, // ID } layui.layedit.set({ uploadImage: { url: BaseUrl + '/api/inventory_records/upload?' //接口url , type: 'post' //默认post } }); var path1 function getpath() { var list_data = JSON.parse(sessionStorage.list_data) for (var i = 0; i < list_data.length; i++) { var o = list_data[i]; if (o.path === "/inventory_records/table") { path1 = o.path $get_power(o.path) } } } getpath() /** * 注册时是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 * @param {String} path 路径 */ function $check_register_field(action, field, path1) { var o = $get_power(path1); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 */ function $check_field(action, field) { var o = $get_power("/inventory_records/view"); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 获取路径对应操作权限 鉴权 * @param {String} action 操作名 */ function $check_exam(path1, action = "get") { var o = $get_power(path1); if (o) { var option = JSON.parse(o.option); if (option[action]) return true } return false; } function $check_option(path,op) { var o = $get_power(path); if (o){ var option = JSON.parse(o.option); if (option[op]) return true } return false; } /** * 是否有审核字段的权限 */ function $check_examine() { var url = window.location.href; var url_ = url.split("/") var pg_url = url_[url_.length - 2] let path = "/"+ pg_url + "/table" var o = $get_power(path); if (o){ var option = JSON.parse(o.option); if (option.examine) return true } return false; } if ($check_field('add', 'warehouse_name')){ $("#warehouse_name_box").show() }else { $("#warehouse_name_box").hide() } if ($check_field('add', 'goods_number')){ $("#goods_number_box").show() }else { $("#goods_number_box").hide() } if ($check_field('add', 'name_of_goods')){ $("#name_of_goods_box").show() }else { $("#name_of_goods_box").hide() } if ($check_field('add', 'classification_of_goods')){ $("#classification_of_goods_box").show() }else { $("#classification_of_goods_box").hide() } if ($check_field('add', 'inventory_of_goods')){ $("#inventory_of_goods_box").show() }else { $("#inventory_of_goods_box").hide() } if ($check_field('add', 'storage_location')){ $("#storage_location_box").show() }else { $("#storage_location_box").hide() } if ($check_field('add', 'employee_users')){ $("#employee_users_box").show() }else { $("#employee_users_box").hide() } if ($check_field('add', 'employee_name')){ $("#employee_name_box").show() }else { $("#employee_name_box").hide() } if ($check_field('add', 'employee_id')){ $("#employee_id_box").show() }else { $("#employee_id_box").hide() } if ($check_field('add', 'inventory_date')){ $("#inventory_date_box").show() }else { $("#inventory_date_box").hide() } if ($check_field('add', 'physical_inventory')){ $("#physical_inventory_box").show() }else { $("#physical_inventory_box").hide() } if ($check_field('add', 'inventory_notes')){ $("#inventory_notes_box").show() }else { $("#inventory_notes_box").hide() } // 日期选择 laydate.render({ elem: '#inventory_date' , format: 'yyyy-MM-dd' , done: function (value) { form_data2.inventory_date = value + ' 00:00:00' } }); async function list_employee_users() { var employee_users = document.querySelector("#employee_users") var op1 = document.createElement("option"); op1.value = '0' employee_users.appendChild(op1) // 收集数据 长度 var count // 收集数据 数组 var arr = [] $.ajax({ url: BaseUrl + '/api/user/get_list?user_group=员工', type: 'GET', contentType: 'application/json; charset=UTF-8', async: false, dataType: 'json', success: function (response) { count = response.result.count arr = response.result.list } }) for (var i = 0; i < arr.length; i++) { var op = document.createElement("option"); // 给节点赋值 op.innerHTML = arr[i].username + "--" + arr[i].nickname op.value = arr[i].user_id // 新增/Add节点 employee_users.appendChild(op) if (form_data2.employee_users==arr[i].employee_users){ op.selected = true } layui.form.render("select"); } } layui.form.on('select(employee_users)', function (data) { form_data2.employee_users = Number(data.elem[data.elem.selectedIndex].value); }) list_employee_users() //文本 let warehouse_name = document.querySelector("#warehouse_name") warehouse_name.onkeyup = function (event) { form_data2.warehouse_name = event.target.value } //文本 //文本 let goods_number = document.querySelector("#goods_number") goods_number.onkeyup = function (event) { form_data2.goods_number = event.target.value } //文本 //文本 let name_of_goods = document.querySelector("#name_of_goods") name_of_goods.onkeyup = function (event) { form_data2.name_of_goods = event.target.value } //文本 //文本 let classification_of_goods = document.querySelector("#classification_of_goods") classification_of_goods.onkeyup = function (event) { form_data2.classification_of_goods = event.target.value } //文本 //数字 let inventory_of_goods = document.querySelector("#inventory_of_goods") inventory_of_goods.onkeyup = function (event) { form_data2.inventory_of_goods = Number(event.target.value) } //数字 //文本 let storage_location = document.querySelector("#storage_location") storage_location.onkeyup = function (event) { form_data2.storage_location = event.target.value } //文本 //文本 let employee_name = document.querySelector("#employee_name") employee_name.onkeyup = function (event) { form_data2.employee_name = event.target.value } //文本 //文本 let employee_id = document.querySelector("#employee_id") employee_id.onkeyup = function (event) { form_data2.employee_id = event.target.value } //文本 //数字 let physical_inventory = document.querySelector("#physical_inventory") physical_inventory.onkeyup = function (event) { form_data2.physical_inventory = Number(event.target.value) } //数字 //多文本 let inventory_notes = document.querySelector("#inventory_notes") //多文本 var data = sessionStorage.data || '' if (data !== '') { var data2 = JSON.parse(data) Object.keys(form_data2).forEach(key => { Object.keys(data2).forEach(dbKey => { if (key === dbKey) { if (key!=='examine_state' && key!=='examine_reply'){ $('#' + key).val(data2[key]) form_data2[key] = data2[key] $('#' + key).attr('disabled', 'disabled') for (let key in form_data2) { if (key == 'employee_users') { let alls = document.querySelector('#employee_users').querySelectorAll('option') let test = form_data2[key] for (let i = 0; i < alls.length; i++) { layui.form.render("select"); if (alls[i].value == test) { alls[i].selected = true form_data2.employee_users = alls[i].value layui.form.render("select"); } } } } } } if(dbKey === "source_table"){ form_data2.source_table = data2[dbKey]; } if(dbKey === "source_id"){ form_data2.source_id = data2[dbKey]; } if(dbKey === "source_user_id"){ form_data2.source_user_id = data2[dbKey]; } }) }) sessionStorage.removeItem("data"); } async function axios_get_4() { if(user_group !='管理员'){ const {data: rese} = await axios.get(BaseUrl + '/api/user/get_list?user_group=' + user_group) let data = rese.result.list const {data: ress} = await axios.get(BaseUrl + '/api/user_group/get_obj?name=' + user_group) const {data: res} = await axios.get(BaseUrl + '/api/' + ress.result.obj.source_table + '/get_obj?user_id=' + use_id) Object.keys(form_data2).forEach(key => { Object.keys(res.result.obj).forEach(dbKey => { if (key === dbKey) { if (key!=='examine_state' && key!=='examine_reply'){ $('#' + key).val(res.result.obj[key]) form_data2[key] = res.result.obj[key] $('#' + key).attr('disabled', 'disabled') } } }) }) for (let key in res.result.obj) { if (key == 'user_id') { let alls = document.querySelector('#employee_users').querySelectorAll('option') let test = res.result.obj.user_id for (let i = 0; i < alls.length; i++) { if (alls[i].value == test) { alls[i].selected = true $('#employee_users').attr('disabled', 'disabled') form_data2.employee_users = alls[i].value layui.form.render("select"); } } } } } } axios_get_4() if (inventory_records_id !== '') { $('#print').show(); async function axios_get_3() { const {data: rese} = await axios.get(BaseUrl + '/api/inventory_records/get_obj', { params: { inventory_records_id: inventory_records_id }, headers: { 'x-auth-token': token //向服务端发送 GET 请求,获取库存记录详情数据 } }) let data = rese.result.obj //将服务端返回的 data 对象同步到前端表单的以下两个位置 Object.keys(form_data2).forEach((key) => { form_data2[key] = data[key]; $("#"+key).val(form_data2[key]) }); for (let key in data) { if (key == 'employee_users') { let alls = document.querySelector('#employee_users').querySelectorAll('option') let test = data[key] for (let i = 0; i < alls.length; i++) { layui.form.render("select"); if (alls[i].value == test) { alls[i].selected = true form_data2.employee_users = alls[i].value layui.form.render("select"); } } } } if ($check_field('set', 'warehouse_name') || $check_field('get', 'warehouse_name')){ $("#warehouse_name_box").show() }else { $("#warehouse_name_box").hide() } if ($check_field('set', 'goods_number') || $check_field('get', 'goods_number')){ $("#goods_number_box").show() }else { $("#goods_number_box").hide() } if ($check_field('set', 'name_of_goods') || $check_field('get', 'name_of_goods')){ $("#name_of_goods_box").show() }else { $("#name_of_goods_box").hide() } if ($check_field('set', 'classification_of_goods') || $check_field('get', 'classification_of_goods')){ $("#classification_of_goods_box").show() }else { $("#classification_of_goods_box").hide() } if ($check_field('set', 'inventory_of_goods') || $check_field('get', 'inventory_of_goods')){ $("#inventory_of_goods_box").show() }else { $("#inventory_of_goods_box").hide() } if ($check_field('set', 'storage_location') || $check_field('get', 'storage_location')){ $("#storage_location_box").show() }else { $("#storage_location_box").hide() } if ($check_field('set', 'employee_users') || $check_field('get', 'employee_users')){ $("#employee_users_box").show() }else { $("#employee_users_box").hide() } if ($check_field('set', 'employee_name') || $check_field('get', 'employee_name')){ $("#employee_name_box").show() }else { $("#employee_name_box").hide() } if ($check_field('set', 'employee_id') || $check_field('get', 'employee_id')){ $("#employee_id_box").show() }else { $("#employee_id_box").hide() } if ($check_field('set', 'inventory_date') || $check_field('get', 'inventory_date')){ $("#inventory_date_box").show() }else { $("#inventory_date_box").hide() } if ($check_field('set', 'physical_inventory') || $check_field('get', 'physical_inventory')){ $("#physical_inventory_box").show() }else { $("#physical_inventory_box").hide() } if ($check_field('set', 'inventory_notes') || $check_field('get', 'inventory_notes')){ $("#inventory_notes_box").show() }else { $("#inventory_notes_box").hide() } // Array.prototype.slice.call(document.getElementsByTagName('input')).map(i => i.disabled = true) // Array.prototype.slice.call(document.getElementsByTagName('select')).map(i => i.disabled = true) // Array.prototype.slice.call(document.getElementsByTagName('textarea')).map(i => i.disabled = true) //文本 warehouse_name.value = form_data2.warehouse_name //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'warehouse_name')) || (!form_data2['inventory_records_id'] && $check_field('add', 'warehouse_name'))) { }else { $("#warehouse_name").attr("disabled", true); $("#warehouse_name > input[name='file']").attr('disabled', true); } //文本 goods_number.value = form_data2.goods_number //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'goods_number')) || (!form_data2['inventory_records_id'] && $check_field('add', 'goods_number'))) { }else { $("#goods_number").attr("disabled", true); $("#goods_number > input[name='file']").attr('disabled', true); } //文本 name_of_goods.value = form_data2.name_of_goods //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'name_of_goods')) || (!form_data2['inventory_records_id'] && $check_field('add', 'name_of_goods'))) { }else { $("#name_of_goods").attr("disabled", true); $("#name_of_goods > input[name='file']").attr('disabled', true); } //文本 classification_of_goods.value = form_data2.classification_of_goods //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'classification_of_goods')) || (!form_data2['inventory_records_id'] && $check_field('add', 'classification_of_goods'))) { }else { $("#classification_of_goods").attr("disabled", true); $("#classification_of_goods > input[name='file']").attr('disabled', true); } //数字 inventory_of_goods.value = form_data2.inventory_of_goods //数字 if ((form_data2['inventory_records_id'] && $check_field('set', 'inventory_of_goods')) || (!form_data2['inventory_records_id'] && $check_field('add', 'inventory_of_goods'))) { }else { $("#inventory_of_goods").attr("disabled", true); $("#inventory_of_goods > input[name='file']").attr('disabled', true); } //文本 storage_location.value = form_data2.storage_location //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'storage_location')) || (!form_data2['inventory_records_id'] && $check_field('add', 'storage_location'))) { }else { $("#storage_location").attr("disabled", true); $("#storage_location > input[name='file']").attr('disabled', true); } if ((form_data2['inventory_records_id'] && $check_field('set', 'employee_users')) || (!form_data2['inventory_records_id'] && $check_field('add', 'employee_users'))) { }else { $("#employee_users").attr("disabled", true); $("#employee_users > input[name='file']").attr('disabled', true); } //文本 employee_name.value = form_data2.employee_name //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'employee_name')) || (!form_data2['inventory_records_id'] && $check_field('add', 'employee_name'))) { }else { $("#employee_name").attr("disabled", true); $("#employee_name > input[name='file']").attr('disabled', true); } //文本 employee_id.value = form_data2.employee_id //文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'employee_id')) || (!form_data2['inventory_records_id'] && $check_field('add', 'employee_id'))) { }else { $("#employee_id").attr("disabled", true); $("#employee_id > input[name='file']").attr('disabled', true); } if ((form_data2['inventory_records_id'] && $check_field('set', 'inventory_date')) || (!form_data2['inventory_records_id'] && $check_field('add', 'inventory_date'))) { }else { $("#inventory_date").attr("disabled", true); $("#inventory_date > input[name='file']").attr('disabled', true); } //数字 physical_inventory.value = form_data2.physical_inventory //数字 if ((form_data2['inventory_records_id'] && $check_field('set', 'physical_inventory')) || (!form_data2['inventory_records_id'] && $check_field('add', 'physical_inventory'))) { }else { $("#physical_inventory").attr("disabled", true); $("#physical_inventory > input[name='file']").attr('disabled', true); } //多文本 inventory_notes.value = form_data2.inventory_notes //多文本 if ((form_data2['inventory_records_id'] && $check_field('set', 'inventory_notes')) || (!form_data2['inventory_records_id'] && $check_field('add', 'inventory_notes'))) { }else { $("#inventory_notes").attr("disabled", true); $("#inventory_notes > input[name='file']").attr('disabled', true); } inventory_date.value = layui.util.toDateString(form_data2.inventory_date, "yyyy-MM-dd") layui.form.render("select"); } axios_get_3() } submit.onclick = async function () { try { //文本 form_data2.warehouse_name = warehouse_name.value //文本 //文本 form_data2.goods_number = goods_number.value //文本 //文本 form_data2.name_of_goods = name_of_goods.value //文本 //文本 form_data2.classification_of_goods = classification_of_goods.value //文本 //数字 form_data2.inventory_of_goods = Number(inventory_of_goods.value) //数字 //文本 form_data2.storage_location = storage_location.value //文本 //文本 form_data2.employee_name = employee_name.value //文本 //文本 form_data2.employee_id = employee_id.value //文本 if(form_data2.inventory_date == ""){ form_data2.inventory_date = (new Date()).toLocaleDateString(); } form_data2.inventory_date = layui.util.toDateString(form_data2.inventory_date, "yyyy-MM-dd") //数字 form_data2.physical_inventory = Number(physical_inventory.value) //数字 //多文本 form_data2.inventory_notes = inventory_notes.value //多文本 } catch (err) { console.log(err) } if (!form_data2.inventory_date){ layer.msg('盘点日期不能为空'); return; } let customize_field = [] customize_field.push({"field_name": "仓库名称", "field_value": form_data2.warehouse_name}); customize_field.push({"field_name": "货物编号", "field_value": form_data2.goods_number}); customize_field.push({"field_name": "货物名称", "field_value": form_data2.name_of_goods}); customize_field.push({"field_name": "货物分类", "field_value": form_data2.classification_of_goods}); customize_field.push({"field_name": "货物库存", "field_value": form_data2.inventory_of_goods}); customize_field.push({"field_name": "存放位置", "field_value": form_data2.storage_location}); customize_field.push({"field_name": "员工", "field_value": form_data2.employee_users}); customize_field.push({"field_name": "员工姓名", "field_value": form_data2.employee_name}); customize_field.push({"field_name": "员工工号", "field_value": form_data2.employee_id}); customize_field.push({ "field_name": "盘点日期", "field_value": form_data2.inventory_date, "type": "date" }); customize_field.push({"field_name": "盘点数量", "field_value": form_data2.physical_inventory}); customize_field.push({"field_name": "盘点备注", "field_value": form_data2.inventory_notes}); if (inventory_records_id == '') { console.log("新增/Add") const {data: res} = await axios.post(BaseUrl + '/api/inventory_records/add?', form_data2, { headers: { 'x-auth-token': token, 'Content-Type': 'application/json' } }) if (res.result == 1) { layer.msg('确认完毕'); setTimeout(function () { colseLayer() }, 1000) }else { layer.msg(res.error.message); } } else { console.log("详情/Details") const {data: res} = await axios.post(BaseUrl + '/api/inventory_records/set?inventory_records_id=' + inventory_records_id, form_data2, { headers: { 'x-auth-token': token, 'Content-Type': 'application/json' } }) if (res.result == 1) { layer.msg('确认完毕'); setTimeout(function () { colseLayer() }, 1000) }else { layer.msg(res.error.message); } } } }) ; </script> </html> 分析我给的代码,哪些代码是盘点新增的核心代码,不要自己写
06-04
@{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } @Html.AntiForgeryToken() <style> .layui-form-pane .layui-form-label { width: 150px; } </style> <div class="x-body"> <div class="layui-form layui-form-pane"> <input type="hidden" id="LineId" name="LineId" /> <input type="hidden" id="UsingFlg" name="UsingFlg" /> <input type="hidden" id="UpdDate" name="UpdDate" /> <div class="layui-form-item"> <label class="layui-form-label x-required">产线编号</label> <div class="layui-input-inline" style="width:200px;"> <input type="text" id="LineNo" name="LineNo" data-require="产线编号不能为空" maxlength="30" autocomplete="off" class="layui-input"> </div> <label class="layui-form-label x-required">产线名称</label> <div class="layui-input-inline" style="width:200px;"> <input type="text" id="LineName" name="LineName" data-require="产线名称不能为空" maxlength="50" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label x-required">工厂编号</label> <div class="layui-input-inline" style="width:200px;"> <input type="text" id="FactoryId" name="FactoryId" readonly="readonly" autocomplete="off" class="layui-input rd"> </div> <div class="layui-input-inline" style="width:200px;"> <input type="text" id="FactoryName" name="FactoryName" readonly="readonly" autocomplete="off" class="layui-input rd"> </div> <div class="layui-input-inline" style="width:50px;"> <button class="layui-btn layui-btn-normal" style="padding:0 12px !important;" id="FactoryBtn" name="FactoryBtn" type="button">选择</button> </div> </div> <div class="layui-form-item"> <label class="layui-form-label x-required">返工代码</label> <div class="layui-input-inline" style="width: 560px; "> <input type="text" id="RwkCode" data-require="返工代码不能为空!" maxlength="25" onkeyup="this.value=this.value.replace(/[^a-zA-Z0-9]*$/g,'')" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">备注</label> <div class="layui-input-inline" style="width: 560px;"> <textarea id="Remark" name="Remark" maxlength="100" placeholder="" class="layui-textarea"></textarea> </div> </div> @*<div class="layui-form-item"> <label class="layui-form-label">周数据链接</label> <div class="layui-input-inline" style="width: 500px;"> <input type="text" id="Week" name="Week" maxlength="50" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">月数据链接</label> <div class="layui-input-inline" style="width: 500px;"> <input type="text" id="Month" name="Month" maxlength="50" class="layui-input "> </div> </div>*@ <div class="layui-form-item" style="position: fixed; bottom: 30px; width: 100%;"> <div class="layui-row"> <div class="layui-col-xs3 layui-col-xs-offset5"> <button class="layui-btn" id="btnSubmit">保存</button> <button class="layui-btn" id="btnCancel">关闭</button> </div> </div> </div> </div> </div> <script> layui.use(['tree','table','util','form'], function () { var tree = layui.tree; var layer = layui.layer; var util = layui.util; var table = layui.table; var form = layui.form; var LineId = '@Html.Raw(@ViewBag.LineId)'; //加载中。。。。。 loading(); // 初始化回调 function initCallBack(data) { if (data.success == false) { alertMessage(data.message); closeLoading(); x_admin_close(); return; } if (LineId != "") { $("#LineNo").attr('readonly', 'readonly'); $("#LineNo").addClass("rd"); var dProdLine = JSON.parse(data.data)[0]; $("#LineNo").val(dProdLine.LINE_NO); $("#LineName").val(dProdLine.LINE_NAME); $("#FactoryId").val(dProdLine.FACTORYID); $("#FactoryName").val(dProdLine.FACTORYNAME); $("#RwkCode").val(dProdLine.RWKCODE); $("#Remark").val(dProdLine.REMARK); //$("#Week").val(dProdLine.WEEK); //$("#Month").val(dProdLine.MONTH); $("#LineId").val(dProdLine.LINE_ID); $("#UsingFlg").val(dProdLine.USINGFLG); $("#UpdDate").val(dProdLine.UPD_DATE); $("#FactoryBtn").hide(); } closeLoading(); } // 初始化 ajaxRequest({ LineId: LineId }, '../ProdLine/ProdLineInit', initCallBack); // 工厂选择 $("#FactoryBtn").click(function () { x_admin_show('工厂选择', '../Dialog/FactoryList?source=1'); }) // 工厂选择回调 window.callBackFactorylist = function (data) { $("#FactoryId").val(data.FACTORYID); $("#FactoryName").val(data.FACTORYNAME); } // 保存 $("#btnSubmit").click(function () { if (!requireCheck(false)) { return false; } layer.confirm('您确定要保存产线信息?', { btn: ['确定', '取消'] //按钮 }, function (index) { layer.close(index); loading(); //M端产线信息 var para = {}; para.LINE_ID = $("#LineId").val(); para.FACTORYID = $("#FactoryId").val(); para.LINE_NO = $("#LineNo").val(); para.LINE_NAME = $("#LineName").val(); para.RWKCODE = $("#RwkCode").val(); para.REMARK = $("#Remark").val(); para.USINGFLG = $("#UsingFlg").val(); para.UPD_DATE_OLD = $("#UpdDate").val(); //para.WEEK = $("#Week").val(); //para.MONTH = $("#Month").val(); ajaxRequest({ prodLineInfo: JSON.stringify(para) }, '../ProdLine/ProdLineAddOrUpdate', saveCallBack); }, function (index) { //取消 }); }); // 保存回调 function saveCallBack(data) { closeLoading(); if (data.success == false) { alertMessage(data.message); } else { window.parent.document.getElementById("btnQuery").click(); x_admin_close(); } } // 取消 $("#btnCancel").click(function () { Close_Dialog(); }); }) </script> 根据这个结构修改我们徐呀的Edit
09-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值