新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
vue指令只能輸入正數(shù)并且只能輸入一個(gè)小數(shù)點(diǎn)的方法-創(chuàng)新互聯(lián)
最近在做一個(gè)金額查詢,驗(yàn)證的時(shí)候總是出現(xiàn)很多問(wèn)題,如輸入-號(hào)后,input框里是沒(méi)有了,但是在model里還是綁定了,提交的時(shí)候就會(huì)報(bào)錯(cuò),真的是讓人很是郁悶,小數(shù)點(diǎn)也是input框過(guò)濾掉了,但是提交的時(shí)候也會(huì)出現(xiàn)。不過(guò)經(jīng)過(guò)努力終于找到了一種解決方案,在這里``和大家分享一下下。
Vue.directive('numbers', { bind: function (el, binding) { console.log('ere') }, inserted: function (el) { // el.querySelector('input').blur(console.log('etset')) }, update: function (el, binding, vnode, oldVnode) { console.log('vnode', vnode) let express = vnode.data.directives[1].expression // let value = el.querySelector('input').value let value = vnode.data.directives[1].value if (typeof value === 'string') {} //在重置的時(shí)候清空 if (value.split('.').length - 1 > 1) { value = value.replace(/\.{2,}/g, '') // 只保留第一個(gè). 清除多余的 value = value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.') } let repeatValue = value.replace(/[^\d.]/g, '') // setTimeout(() => { // el.querySelector('input').value = repeatValue // }, 5) setValueWithExpressionVue(vnode.context.$data, express, repeatValue) }, componentUpdated: function () {}, unbind: function (el) {} }) // function setValueWithExpressionVue (currObj, expression, value) { expression = expression.split('.') expression.forEach(function (arg, i) { if (i < expression.length - 1) { currObj = currObj[arg] } else { currObj[arg] = value } }) }
分享名稱:vue指令只能輸入正數(shù)并且只能輸入一個(gè)小數(shù)點(diǎn)的方法-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://fisionsoft.com.cn/article/cecpco.html