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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天照着网上的《四级密码强度校验》视频写了一个相同的代码,主要作用就是在输入密码后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>


<!-- 盘点新增/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
<!-- 客户信息新增/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="client_company_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="client_company"> </div> </div> <div class="layui-form-item unique" id="customer_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="customer_name"> </div> </div> <div class="layui-form-item select-box" id="customer_gender_box"> <label class="layui-form-label">客户性别</label> <div class="layui-input-block select block"> <select name="interest" lay-filter="customer_gender" id="customer_gender"> <option value=""></option> </select> </div> </div> <div class="layui-form-item" id="customer_phone_number_box"> <label class="layui-form-label">客户电话</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="phone_number" autocomplete="off" placeholder="请输入客户电话" class="layui-input" id="customer_phone_number"> </div> </div> <div class="layui-form-item layui-form-text" id="customer_address_box"> <label class="layui-form-label">客户地址</label> <div class="layui-input-block text"> <textarea placeholder="请输入客户地址" class="layui-textarea" id="customer_address"></textarea> </div> </div> <div class="layui-form-item layui-form-text" id="customer_preferences_box"> <label class="layui-form-label">客户偏好</label> <div class="layui-input-block text"> <textarea placeholder="请输入客户偏好" class="layui-textarea" id="customer_preferences"></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 customer_information_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) { 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('/customer_information/view', 'add') || $check_action('/customer_information/view', 'set') || $check_option('/customer_information/table', 'examine')) { }else { $("#submit").hide() } // style="display: none" let field = "customer_information_id"; let url_add = "customer_information"; let url_set = "customer_information"; let url_get_obj = "customer_information"; let url_upload = "customer_information" let query = { "customer_information_id": 0, } let form_data2 = { "client_company": '', // 客户公司 "customer_name": '', // 客户姓名 "customer_gender": '', // 客户性别 "customer_phone_number": '', // 客户电话 "customer_address": '', // 客户地址 "customer_preferences": '', // 客户偏好 "customer_information_id": 0, // ID } layui.layedit.set({ uploadImage: { url: BaseUrl + '/api/customer_information/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 === "/customer_information/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("/customer_information/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', 'client_company')){ $("#client_company_box").show() }else { $("#client_company_box").hide() } if ($check_field('add', 'customer_name')){ $("#customer_name_box").show() }else { $("#customer_name_box").hide() } if ($check_field('add', 'customer_gender')){ $("#customer_gender_box").show() }else { $("#customer_gender_box").hide() } if ($check_field('add', 'customer_phone_number')){ $("#customer_phone_number_box").show() }else { $("#customer_phone_number_box").hide() } if ($check_field('add', 'customer_address')){ $("#customer_address_box").show() }else { $("#customer_address_box").hide() } if ($check_field('add', 'customer_preferences')){ $("#customer_preferences_box").show() }else { $("#customer_preferences_box").hide() } // 客户性别选项列表 let customer_gender_data = ['男','女']; async function customer_gender() { var customer_gender = document.querySelector("#customer_gender") var op1 = document.createElement("option"); op1.value = '0' customer_gender.appendChild(op1) // 收集数据 长度 var count // 收集数据 数组 var arr = [] count = customer_gender_data.length arr = customer_gender_data for (var i = 0; i < arr.length; i++) { var op = document.createElement("option"); // 给节点赋值 op.innerHTML = arr[i] op.value = arr[i] // 新增/Add节点 customer_gender.appendChild(op) if (form_data2.customer_gender==arr[i].customer_gender){ op.selected = true } layui.form.render("select"); } } layui.form.on('select(customer_gender)', function (data) { form_data2.customer_gender = data.elem[data.elem.selectedIndex].text; }) customer_gender() //文本 let client_company = document.querySelector("#client_company") client_company.onkeyup = function (event) { form_data2.client_company = event.target.value } //文本 //文本 let customer_name = document.querySelector("#customer_name") customer_name.onkeyup = function (event) { form_data2.customer_name = event.target.value } //文本 //多文本 let customer_address = document.querySelector("#customer_address") //多文本 //多文本 let customer_preferences = document.querySelector("#customer_preferences") //多文本 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 == 'customer_gender') { let alls = document.querySelector('#customer_gender').querySelectorAll('option') let test = form_data2[key] for (let i = 0; i < alls.length; i++) { if (alls[i].innerHTML == test) { alls[i].selected = true form_data2.customer_gender = alls[i].text 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"); } if (customer_information_id !== '') { $('#print').show(); async function axios_get_3() { const {data: rese} = await axios.get(BaseUrl + '/api/customer_information/get_obj', { params: { customer_information_id: customer_information_id }, headers: { 'x-auth-token': token } }) let data = rese.result.obj Object.keys(form_data2).forEach((key) => { form_data2[key] = data[key]; $("#"+key).val(form_data2[key]) }); for (let key in data) { if (key == 'customer_gender') { let alls = document.querySelector('#customer_gender').querySelectorAll('option') let test = data[key] for (let i = 0; i < alls.length; i++) { if (alls[i].innerHTML == test) { alls[i].selected = true form_data2.customer_gender = alls[i].text layui.form.render("select"); } } } } if ($check_field('set', 'client_company') || $check_field('get', 'client_company')){ $("#client_company_box").show() }else { $("#client_company_box").hide() } if ($check_field('set', 'customer_name') || $check_field('get', 'customer_name')){ $("#customer_name_box").show() }else { $("#customer_name_box").hide() } if ($check_field('set', 'customer_gender') || $check_field('get', 'customer_gender')){ $("#customer_gender_box").show() }else { $("#customer_gender_box").hide() } if ($check_field('set', 'customer_phone_number') || $check_field('get', 'customer_phone_number')){ $("#customer_phone_number_box").show() }else { $("#customer_phone_number_box").hide() } if ($check_field('set', 'customer_address') || $check_field('get', 'customer_address')){ $("#customer_address_box").show() }else { $("#customer_address_box").hide() } if ($check_field('set', 'customer_preferences') || $check_field('get', 'customer_preferences')){ $("#customer_preferences_box").show() }else { $("#customer_preferences_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) //文本 client_company.value = form_data2.client_company //文本 if ((form_data2['customer_information_id'] && $check_field('set', 'client_company')) || (!form_data2['customer_information_id'] && $check_field('add', 'client_company'))) { }else { $("#client_company").attr("disabled", true); $("#client_company > input[name='file']").attr('disabled', true); } //文本 customer_name.value = form_data2.customer_name //文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_name')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_name'))) { }else { $("#customer_name").attr("disabled", true); $("#customer_name > input[name='file']").attr('disabled', true); } if ((form_data2['customer_information_id'] && $check_field('set', 'customer_gender')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_gender'))) { }else { $("#customer_gender").attr("disabled", true); $("#customer_gender > input[name='file']").attr('disabled', true); } if ((form_data2['customer_information_id'] && $check_field('set', 'customer_phone_number')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_phone_number'))) { }else { $("#customer_phone_number").attr("disabled", true); $("#customer_phone_number > input[name='file']").attr('disabled', true); } //多文本 customer_address.value = form_data2.customer_address //多文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_address')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_address'))) { }else { $("#customer_address").attr("disabled", true); $("#customer_address > input[name='file']").attr('disabled', true); } //多文本 customer_preferences.value = form_data2.customer_preferences //多文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_preferences')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_preferences'))) { }else { $("#customer_preferences").attr("disabled", true); $("#customer_preferences > input[name='file']").attr('disabled', true); } layui.form.render("select"); } axios_get_3() } submit.onclick = async function () { try { //文本 form_data2.client_company = client_company.value //文本 //文本 form_data2.customer_name = customer_name.value //文本 //文本 form_data2.customer_phone_number = customer_phone_number.value //文本 //多文本 form_data2.customer_address = customer_address.value //多文本 //多文本 form_data2.customer_preferences = customer_preferences.value //多文本 } catch (err) { console.log(err) } let customer_phone_number_phone_regular = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; if(form_data2.customer_phone_number && !customer_phone_number_phone_regular.test(form_data2.customer_phone_number)){ layer.msg('手机号格式错误'); return; } let customize_field = [] customize_field.push({"field_name": "客户公司", "field_value": form_data2.client_company}); customize_field.push({"field_name": "客户姓名", "field_value": form_data2.customer_name}); customize_field.push({"field_name": "客户性别", "field_value": form_data2.customer_gender}); customize_field.push({"field_name": "客户电话", "field_value": form_data2.customer_phone_number}); customize_field.push({"field_name": "客户地址", "field_value": form_data2.customer_address}); customize_field.push({"field_name": "客户偏好", "field_value": form_data2.customer_preferences}); if (customer_information_id == '') { console.log("新增/Add") const {data: res} = await axios.post(BaseUrl + '/api/customer_information/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/customer_information/set?customer_information_id=' + customer_information_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> 逐个分析我给的代码内容,代码对应的功能,不要自己写
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值