国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

vue 限制input只能輸入正數(shù)的操作

瀏覽:93日期:2022-12-16 15:37:56

在某些項(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)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 普兰店市| 论坛| 塘沽区| 五常市| 荥阳市| 沾益县| 云安县| 休宁县| 伊宁市| 衡南县| 金寨县| 海淀区| 元阳县| 和硕县| 凭祥市| 浦县| 兴化市| 石景山区| 西畴县| 九江市| 吴堡县| 长春市| 浦城县| 东光县| 新化县| 安新县| 桂平市| 高邮市| 长治市| 法库县| 磴口县| 松阳县| 丁青县| 调兵山市| 东兰县| 东平县| 汕头市| 宁夏| 儋州市| 昌邑市| 四子王旗|