element el-input directive数字进行控制
网络编程 2021-07-04 16:46www.168986.cn编程入门
本文介绍了vue使用directive 进行控制的方法,使用element开发的过程中遇到循环的数据只能输入数字,并且有不要小数点,有需要小数点的,就有一定的参考价值,有兴趣的可以了解一下
使用自定义指令格式化el-input
背景
使用element开发的过程中遇到循环的数据只能输入数字,并且有不要小数点,有需要小数点的
使用vue directive 进行控制
开发
页面使用方式 v-numberInt:0="item.first_fee" 0为保留几位小数
<tr v-for="(item,index) in form.valuation_rules" :key="index"> <td class="center" > <el-input v-if="form.valuation_type==1" v-numberInt:0="item.first_fee" v-model.trim="item.first_amount"></el-input> <el-input v-else v-model.trim="item.first_amount" v-numberInt:2="item.first_fee"></el-input> </td> <td class="center"> <el-input v-model.trim="item.first_fee" v-numberInt:2="item.first_fee"></el-input> </td> <td class="center"> {{item.additional_amount}} </td> <td class="center"> <el-input v-model.trim="item.additional_fee" v-numberInt:2="item.additional_fee"></el-input> </td> </tr>
因为用的是element 的el-input ,组件input外层包着一层div所以要使用const element = el.getElementsByTagName('input')[0]获取 input对其监听失焦 当输入的不是数字时,失焦后会变成0,没有使用directive update方法,比较简单directives.js
directives.js
Vue.directive('numberInt', { bind: function(el, binding, vnode) { const element = el.getElementsByTagName('input')[0] const len = binding.arg // 初始化设置 element.value = Number(element.value ).toFixed(len) // 失焦时候格式化 element.addEventListener('blur', function() { if (isNaN(element.value)) { vnode.data.model.callback(0) } else { vnode.data.model.callback(Number(element.value).toFixed(len)) } }) }})
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指