【js+css】经纬度输入框(ip输入框)

该博客介绍了一个项目需求,即制作一个类似系统输入IP地址的验证输入框。通过结合三个输入框并定制校验规则,实现了对经纬度的输入控制,包括输入范围限制和焦点切换。提供的代码示例展示了如何使用JavaScript进行键盘事件监听和输入验证,确保输入值在指定范围内。

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

1.项目里的需求要做一个类似系统输入ip的校验框
(ip校验框)只需要多加一个输入框,修改校验规则,我这里只展示了经纬度输入框的代码
2.思路:

  • 3个input合成一个输入框
  • 给输入框做校验
  • 绑定键盘,焦点事件

3.效果展示:
请添加图片描述

4.完整代码

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		<title>自动获取光标输入框</title>
		<style>
			.ip_input  {
					width:20%;
					font-size: 14px;
					color:#000000; 
					border:0px; 
					text-align:center;
					 position:relative; 
					 top:1px; 
					 padding: 6px 0px;
					 outline:none;
				}
			 .all_input  {
				width:160px;
				height:30px;
				font:normal 8px 宋体; 
				color:#676a6c; 
				border:1px solid #ccc; 
				text-align:center; 
				border-radius: 3px;
			}
			form{
				display: flex;
				
			}
		</style>
	</head>
	<body>
		
		<script language=javascript>

			function keyUpEvent(obj, num,maxNum) {
				let input1=document.getElementsByClassName("ip1")[num]
				let input2=document.getElementsByClassName("ip2")[num]
				let input3=document.getElementsByClassName("ip3")[num]
				code = event.keyCode
				if (obj == 'ip1') {
					if (input1.value > maxNum) {
						alert("填写范围必须在 0 - "+maxNum+"间");
						input1.value = input1.value.substring(0, input1.value.length - 1);
						return;
					}
					if(maxNum==180){
						
						if (input1.value.length >= 3 && code != 37 && code != 39 && code != 16 && code != 9 && code != 13)
							input2.focus();
					}else{
						if (input1.value.length >= 2 && code != 37 && code != 39 && code != 16 && code != 9 && code != 13)
							input2.focus();
					}
				}
				if (obj == 'ip2') {
					if (input2.value >= 60) {
						alert("填写范围必须在 0 - 60间");
						input2.value = input2.value.substring(0, input2.value.length - 1);
						return;
					}
					if (input2.value.length >= 2 && code != 37 && code != 39 && code != 16 && code != 9 && code != 13)
						input3.focus();
				}
				
			}

			function keyUpEventForIp3(obj,num) {
				let input3=document.getElementsByClassName(obj)[num]
				if (input3.value >= 60) {
					alert("填写范围必须在 0 - 60间");
					input3.value = input3.value.substring(0, input3.value.length - 1);
					return;
				}

			}
		</script>

			<div class=all_input>
				<input name='ip1' class='ip1 ip_input' maxlength=3 autocomplete="off"
					onkeyup='keyUpEvent("ip1",0,180)'>°
				<input name='ip2' class='ip2 ip_input' maxlength=2 autocomplete="off" 
					onkeyup='keyUpEvent("ip2",0)'><input name='ip3' class='ip3 ip_input' maxlength=2 autocomplete="off" 
					onkeyup='keyUpEventForIp3("ip3",0)'></div>
			<div class=all_input>
				<input name='ip4' class='ip1 ip_input' maxlength=3 autocomplete="off"
					onkeyup='keyUpEvent("ip1",1,90)'>°
				<input name='ip5' class='ip2 ip_input' maxlength=2 autocomplete="off" 
					onkeyup='keyUpEvent("ip2",1)'><input name='ip6' class='ip3 ip_input' maxlength=2 autocomplete="off" 
					onkeyup='keyUpEventForIp3("ip3",1)'></div>
	</body>
</html>

在于GIS相关的软件中经常要输入经纬度数值,而基于MFC Edit控件的经纬度输入控件需 要创建三个Edit编辑框,来分别输入度、分、秒数值,并且需要分别进行输入范围和有效 性的检验。该基于BCGControlBar的经纬度输入控件实现了在一个输入窗口中分别按照度 、分、秒三个值域来输入经纬度数值,并同时进行了输入分有效性和输入范围的检验。经 纬度中的度、分、秒字段均通过数字键盘直接输入数值,也可通过微调按钮或者键盘的上 下键进行数字的增减,你可以通过左右键进行度、分、秒输入域的切换。该控件可以设置 度的输入范围,默认的度范围是0~89。该1.1版本修复了键盘直接输入数值的逻辑问题。 In the GIS-related software often have to enter the latitude and longitude values, and MFC Edit control based on the latitude and longitude input controls need to create three Edit edit box to enter the degrees, minutes and seconds values, and the need for input range and validity of the test. The BCGControlBar-based latitude and longitude input control realizes the input of the latitude and longitude values in degrees,minutes,and seconds in one input window, and simultaneously verifies the validity of the input points and the input range.The degrees, minutes, and seconds fields can be entered directly through the numeric keypad, also can be fine-tuning button or the keyboard up and down keys to increase or decrease the number, you can use left and right keys for degrees, minutes and seconds to switch the input field. The control can set the input range of degree, the default range is 0 ~ 89.The 1.1 version fixes the logic of keyboard input values directly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值