vue项目移动端实现ip输入框问题

作者:肥皂泡 时间:2024-05-09 09:20:58 

vue框架移动端做ip输入框组件,input在浏览器和微信端兼容问题。

要求:只能输入数字,输入数字以外的字符(包括点、冒号等数字符号)时自动跳到下一段ip输入框.

type=number类型,不会禁止点的输入。手动过滤拿不到包括点字符的字符串.而且输入多个点之后,拿到的值为空.

解决办法:type=tel,只能输入数字,且可以获取到点字符的输入

问题:微信下keyup事件无效,回调事件中event.keyCode返回全是229.

解决办法:监听input事件,event事件对象中keycode为空,但是event.data返回输入字符,可以实现过滤.


<template>
 <div class="ipAdress">
   <ul class="ipInput" :class="{isDisabled:isDisabled}" >
     <li :key='index' v-for="(item,index) in ipAdress">
       <input :tabindex="'ipInput'+(index+1)" :class="'ipAdress'+(index+1)" @blur="blurFocus(index)" autocomplete="off" :readonly="isDisabled" maxlength="3" type="tel" pattern="[0-9]{1,3}" @input="checkIpVal(item,index,$event)" :disabled="isDisabled" @keyup="turnIpPOS(item,index,$event)" @keydown="delteIP(item,index,$event)" v-model="item.value" ref="ipInput" />
       <span v-if="index<3">.</span>
     </li>
   </ul>
 </div>
</template>

<script>
 export default {
   data() {
     return {
       ipAdress: [{
         value: ''
       }, {
         value: ''
       }, {
         value: ''
       }, {
         value: ''
       }],
       isWX:navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger"
     };
   },
   props: {
     ipStr: {
       trype: String,
       default: ''
     },
     ipType: {
       type: String
     },
     isDisabled: {
       type: Boolean,
       default: false
     },
     width: {
       type: String,
       default:'100%'
     }
   },
   watch: {
     ipStr:{
       immediate:true,
       handler:function(vall) {
         let val = vall;
         let nArr = [];
         if(val && val.includes('.') && val.length > 0) {
           let valArr = val.split('.');
           let m = valArr.length;
           for(let i = 0; i < 4; i++) {
             if(valArr[i] == 'null' || valArr[i] == 'undefined'){
               valArr[i] = '';
             }
             if(i < m) {
               nArr.push({
                 value: valArr[i]
               });
             } else {
               nArr.push({
                 value: ''
               });
             }
           }
         } else {
           nArr = [{
             value: ''
           }, {
             value: ''
           }, {
             value: ''
           }, {
             value: ''
           }];
         }
         this.ipAdress = nArr;
       }
     }
   },
   methods: {
     //methods
     blurFocus(index) {
       if(index == 3) {
         this.$emit('blur');
       }
     },
     checkIpVal(item,index,event) {
       let self = this;
       //wx
       if(this.isWX){
         let e = event || window.event;
         let keyCode = e.data;

//           //.向右跳转
         if(keyCode === ".") {
           e.preventDefault();
           e.returnValue = false;
           item.value = item.value.replace(/[^\d]/g, "").replace(/[\.]/g, "");
           if(index < 3 ) {
             self.$refs.ipInput[index + 1].focus();
           }
           return false;
         }
       }

let isNo = /^[0-9]{1,3}$/g;
       if(/[^\d]/g.test(item.value)){
         let cache = JSON.parse(JSON.stringify(self.ipAdress));
         cache[index].value = item.value.replace(/[^\d]/g, "").replace(/[\.]/g, "");
         self.ipAdress = cache;
         return false;
       }

if(item.value.replace(/[^\d]/g, "").length >= 3) {        
         let val = parseInt(item.value.replace(/[^\d]/g, ""), 10);
         if(isNaN(val)) {
           val = ''
         } else if(val > 255) {
           val = 255;
         } else {
           val = val < 0 ? 0 : val;
         }
         item.value = String(val);
         this.$set(this.ipAdress,index,item);
         if(index < 3 ) {            
           self.$refs.ipInput[index + 1].focus();                
         }
       }      
       let ns = '';
       this.ipAdress.forEach(item => ns += '.' + item.value);
       if(ns.length <= 4){
         this.$emit('getIP', "", this.ipType);
       }else{
         this.$emit('getIP', ns.slice(1), this.ipType);
       }

},
     turnIpPOS(item, index, event) {
       let self = this;
       let e = event || window.event;

if(e.keyCode == 37) {
         if(index != 0) {
           self.$refs.ipInput[index - 1].focus();
         }
       }
       //右箭头、回车键、空格键、冒号均向右跳转,右一不做任何措施
       if(e.keyCode == 39 || e.keyCode == 13 || e.keyCode == 32 || e.keyCode == 110 || e.keyCode == 46 || e.keyCode == 190 ) {
         e.preventDefault();
         e.returnValue = false;
         if(index < 3 ) {
           self.$refs.ipInput[index + 1].focus();
         }
         return false;
       }

},
     delteIP(item, index, event) {  
       let self = this;
       let e = event || window.event;

let val = parseInt(item.value.replace(/[^\d]/g, ""), 10);
       val = isNaN(val) ? '' : val;
       if(e.keyCode == 8 && index > 0 && val.length==0) {
           self.$refs.ipInput[index - 1].focus();
       }
     }
   },
   mounted(){
     console.log(this.$props)
     console.log(this.$attrs.index)
   }
 };
</script>

<style lang="scss" scoped>
 $--border-color:#ccc;
 $--outline-color:transparent;
 $--font-color:$--input-color;
 $base-font-size:12px;
 .ipInput {
   box-sizing: border-box;
   line-height: inherit;
   border: 1px solid $--border-color;
   overflow: hidden;
   border-radius: 5px;
   padding: 0;
   margin: 0;
   display: inline-block;
   vertical-align: middle;
   outline: $--outline-color;
   font-size:0;
   text-indent: 0;
   background: #fff;
   &.isDisabled {
     background: $--outline-color;

li{
       cursor:not-allowed;
       input{
         cursor:not-allowed;
       }
     }
   }
   li {
     display: inline-block;
     width:25%;
     box-sizing: border-box;
     font-size:0;
     input {
       appearance: none;
       padding:10px 5px;
       width: calc(100% - 3px);
       text-align: center;
       outline: none;
       border: none;
       color: $--font-color;
       box-sizing: border-box;
       font-size: $base-font-size;
       &:disabled {
         background: $--outline-color;
       }
     }
     span {
       display: inline-block;
       font-size:$base-font-size;
       width: 3px;
       color: $--font-color;
     }
   }
 }
</style>

来源:https://segmentfault.com/a/1190000018559436

标签:vue,移动端,ip输入框
0
投稿

猜你喜欢

  • pytorch中使用cuda扩展的实现示例

    2021-02-17 23:46:55
  • windows10更换mysql8.0.17详细教程

    2024-01-26 19:56:19
  • Python用 KNN 进行验证码识别的实现方法

    2021-03-29 06:58:08
  • mysql 如何使用JSON_EXTRACT() 取json值

    2024-01-16 04:26:46
  • jupyter notebook运行命令显示[*](解决办法)

    2022-02-19 01:23:10
  • PHP概率计算函数汇总

    2023-11-19 08:06:19
  • Python 生成多行重复数据的方法实现

    2022-07-26 18:56:07
  • pycharm from lxml import etree标红问题及解决

    2021-12-10 05:55:38
  • vue 自定义右键样式的实例代码

    2023-07-02 16:33:34
  • Python基于Pymssql模块实现连接SQL Server数据库的方法详解

    2024-01-15 03:13:17
  • jquery弹出层背景变暗 Lee dialog

    2008-08-18 13:11:00
  • Python趣味挑战之教你用pygame画进度条

    2022-08-13 15:02:49
  • 听歌识曲--用python实现一个音乐检索器的功能

    2021-11-01 00:46:03
  • 用ASP实现域名绑定子目录

    2009-03-11 13:28:00
  • python设置中文界面实例方法

    2023-08-30 18:56:30
  • Javascript中实现trim()函数的两种方法

    2024-04-17 10:38:38
  • pygame实现方块动画实例讲解

    2022-11-01 13:37:46
  • python 使用socket传输图片视频等文件的实现方式

    2022-11-12 11:55:37
  • Python docutils文档编译过程方法解析

    2023-01-20 16:41:23
  • Python实现单例模式的5种方法

    2021-07-13 19:40:08
  • asp之家 网络编程 m.aspxhome.com