美文网首页
判断一个input框的输入内容是纯数字

判断一个input框的输入内容是纯数字

作者: Luv_coco | 来源:发表于2019-06-12 20:22 被阅读0次

vue后台管理有个搜索功能,一个input框里集合了两种搜索,既能按搜索编号也能按搜索名称,一般编号设置为纯数字

一开始想的是input框内容是string类型,先parseInt()转换,然后typeof检测是不是number类型,发现NaN也是number类型,尴尬了。

created(){
    document.onkeydown = function(e) {//键盘回车触发搜索
      var key = window.event.keyCode;
      if (key == 13) {
        if(this.searchBrand!==''){//输入为空的回车不生效
          if(this.searchBrand/1==this.searchBrand/1){
           //除以1会强制转换类型,为true则纯数字字符串,false则NaN==NaN为false
            console.log('编号')
            this.getBrandList({filter_id:this.searchBrand})
          }else{
            console.log('名称' )
            this.getBrandList({filter_name:this.searchBrand})
          }
        }
      }
    }.bind(this)//让函数里面的this不指向Window,指向vue
},

相关文章

网友评论

      本文标题:判断一个input框的输入内容是纯数字

      本文链接:https://www.haomeiwen.com/subject/hqprfctx.html