zoukankan      html  css  js  c++  java
  • 只能输入金额格式的input

    element el-input 只能输入数字,限制最大最小,小数位数 --使用 directive

    1. 创建onlyNumber指令 src/directive/el-input/onlyNumber.js
    export default {
      inserted(el, vDir, vNode) {
        // vDir.value 有指令的参数
        let content
        // 按键按下=>只允许输入 数字/小数点
        el.addEventListener('keypress', (event) => {
          const e = event || window.event
          const inputKey = String.fromCharCode(typeof e.charCode === 'number' ? e.charCode : e.keyCode)
          const re = /d|./
          content = e.target.value
          // 定义方法,阻止输入
          function preventInput() {
            if (e.preventDefault) {
              e.preventDefault()
            } else {
              e.returnValue = false
            }
          }
          if (!re.test(inputKey) && !e.ctrlKey) {
            preventInput()
          } else if (content.indexOf('.') > 0 && inputKey === '.') {
            // 已有小数点,再次输入小数点
            preventInput()
          }
        })
        // 按键弹起=>并限制最大最小
        el.addEventListener('keyup', (event) => {
          const e = event || window.event
          content = parseFloat(e.target.value)
          if (!content) {
            content = 0.0
          }
          let arg_max = ''
          let arg_min = ''
          if (vDir.value) {
            arg_max = parseFloat(vDir.value.max)
            arg_min = parseFloat(vDir.value.min)
          }
          if (arg_max && content > arg_max) {
            e.target.value = arg_max
            content = arg_max
            e.target.dispatchEvent(new Event('input'))
          }
          if (arg_min && content < arg_min) {
            e.target.value = arg_min
            content = arg_min
            e.target.dispatchEvent(new Event('input'))
          }
        })
        // 失去焦点=>保留指定位小数
        el.addEventListener('focusout', (event) => {
          // 此处会在 el-input 的 @change 后执行
          const e = event || window.event
          content = parseFloat(e.target.value)
          if (!content) {
            content = 0.0
          }
          let arg_precision = 0 // 默认保留至整数
          if (vDir.value.precision) {
            arg_precision = parseFloat(vDir.value.precision)
          }
          e.target.value = content.toFixed(arg_precision)
          e.target.dispatchEvent(new Event('input'))
          // -- callback写法1
          // vNode.data.model.callback = ()=>{
          //     e.target.value = content.toFixed(arg_precision)
          // }
          // vNode.data.model.callback();
          // -- callback 写法2
          // vNode.data.model.callback(
          //     e.target.value = content.toFixed(arg_precision)
          // )
        })
      }
    }

    2. onlyNumber指令导出  src/directive/el-input/index.js

    import onlyNumber from './onlyNumber'
    const install = (Vue) => {
      Vue.directive('onlyNumber', onlyNumber)
    }
    /*
      Vue.use( plugin )
      安装 Vue.js 插件。如果插件是一个对象,必须提供 install 方法。
      如果插件是一个函数,它会被作为 install 方法。install 方法调用时,会将 Vue 作为参数传入。
      该方法需要在调用 new Vue() 之前被调用。
      当 install 方法被同一个插件多次调用,插件将只会被安装一次。
    */

    if (window.Vue) {
      window['onlyNumber'] = onlyNumber
      Vue.use(install); // eslint-disable-line
    }

    onlyNumber.install = install
    export default onlyNumber
     
    3.全局调用  src/main.js
     
    import onlyNumber from '@/directive/el-input'    
    
    Vue.use(onlyNumber)

    4.应用于页面

     <el-input  v-model="amount"  v-onlyNumber="{precision:2,min:0,max:9999}"  type="number" placeholder="请输入金额"  size="mini" ></el-input>
     
  • 相关阅读:
    OSG-提示“error reading file e:1.jpg file not handled”
    OSG-加载地球文件报0x00000005错误,提示error reading file simple.earth file not handled
    QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
    我的书《Unity3D动作游戏开发实战》出版了
    java中无符号类型的第三方库jOOU
    Windows批处理备份mysql数据
    使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A
    Java版本,Java版本MongoDB驱动,驱动与MongoDB数据库,Spring之间的兼容性
    Jrebel本地激活方法
    wget下载指定网站目录下的所有内容
  • 原文地址:https://www.cnblogs.com/Adyblog/p/15384214.html
Copyright © 2011-2022 走看看