vue 限制input只能輸入正數(shù)的操作
在某些項(xiàng)目中 input 框只能輸入數(shù)字,可以用以下辦法:
先在標(biāo)簽上綁定上 @input 事件來(lái)監(jiān)聽(tīng)標(biāo)簽的值變化,通過(guò)正則來(lái)改變輸入的值。
<input v-number-only v-model='scope.row.fileOrder' @input='scope.row.fileOrder = Number($event.target.value.replace(/D+/, ’’))' />
第二部封裝個(gè)自定義指令放在標(biāo)簽上!
directives: { numberOnly: { bind: function(el) { el.handler = function() { el.value = Number(el.value.replace(/D+/, ’’)) } el.addEventListener(’input’, el.handler) }, unbind: function(el) { el.removeEventListener(’input’, el.handler) } } },
接下來(lái)就可以去頁(yè)面看效果了,只能輸入數(shù)字且只是正數(shù)!
附上 element 的 input 樣式代碼
.keep_input { -webkit-appearance: none; background-color: #fff; background-image: none; border-radius: 4px; border: 1px solid #dcdfe6; -webkit-box-sizing: border-box; box-sizing: border-box; color: #606266; display: inline-block; font-size: inherit; outline: 0; padding: 0 15px; -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1); transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1); height: 30px; line-height: 30px; text-align: left; } .keep_input:focus { border-color: #54a6de; outline: 0; }
補(bǔ)充知識(shí):記錄el-input type=number限制長(zhǎng)度el-input使用
如下所示:
<el-input type='number' oninput='if(value.length>10)value=value.slice(0,10)' @keyup.enter.native='query()' onKeypress='return(/[d.]/.test(String.fromCharCode(event.keyCode)))' :max='99999999'> </el-input>
oninput 是個(gè)自定義事件 在事件里面獲取輸入的數(shù)字長(zhǎng)度,來(lái)進(jìn)行判斷如果大于規(guī)定長(zhǎng)度就進(jìn)行剪切。
keyup.enter.native 是個(gè)鍵盤(pán)回車(chē)事件,當(dāng)按下Enter鍵時(shí)觸發(fā)query()事件。
max為輸入框的最大值,如果input的type=number那么輸入框內(nèi)是輸入不了字符的。
number框 解決輸入e的問(wèn)題
主要原因是:e在數(shù)學(xué)上代表的是無(wú)理數(shù),是一個(gè)無(wú)限不循環(huán)的小數(shù),其值約為2.7182818284,所以在輸入e的時(shí)候,輸入框會(huì)把e當(dāng)成一個(gè)數(shù)字看待。
可以采用下面的方式來(lái)避免這個(gè)BUG,在input標(biāo)簽中添加如下屬性:
onKeypress=“return(/[d.]/.test(String.fromCharCode(event.keyCode)))”
<el-input placeholder='請(qǐng)輸入密碼' v-model='input' :show-password='true'></el-input>
show-password 加上這個(gè)屬性輸入字符進(jìn)行隱藏一般用于密碼框使用
記錄問(wèn)題!
以上這篇vue 限制input只能輸入正數(shù)的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. vue使用webSocket更新實(shí)時(shí)天氣的方法4. 淺談python出錯(cuò)時(shí)traceback的解讀5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解9. Nginx+php配置文件及原理解析10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析
