在Vue.js中,如何从输入框后面填充数值。

huangapple go评论78阅读模式
英文:

In Vuejs, how to fill value from input box from behind

问题

我有一个使用 Vue.js 和 Quasar 的项目。

我有一个输入字段,它初始化为10个零,只允许输入数字。
如果我想添加一个数字,它应该从后面填充。

"0000000000" -> 填充1
"0000000001" -> 填充2
"0000000012" -> 填充5
"0000000125" -> 删除最后一个字符
"0000000012" -> 填充3
"0000000123"

有什么想法如何实现这个?这是我目前的代码:

<template>
<div>
<q-input class="input" v-model="number" outlined label="number" mask="##########" minLength="10" maxlength="10" @input="onInput"/>
</div>
</template>

<script>
import { ref } from 'vue'

export default {

setup() {
const number = ref("0000000000")

const onInput = (value) => {
if (value.length > 10) {
// 如果输入的数字超过了10位,删除多余的部分
value = value.slice(0, 10);
}
const numericValue = parseInt(value);
if (!isNaN(numericValue)) {
// 如果是有效的数字,更新输入字段的值
number.value = numericValue.toString().padStart(10, '0');
}
}

return {
number,
onInput
}
}
</script>


onInput函数现在应该按照您的要求进行填充和删除操作。
英文:

i have a project using Vue js and Quasar.

I have an input field that is initialized with 10 zeros that only allows numeric numbers.
If I want to add a number, it should fill it in from behind.

"0000000000" -> fill in 1

"0000000001" -> fill in 2

"0000000012" -> fill in 5

"0000000125" -> delete last character

"0000000012" -> fill in 3

"0000000123"

any ideas how i can achieve this? This is what i have currently:

&lt;template&gt;
&lt;div&gt; 
&lt;q-input class=&quot;input&quot; v-model=&quot;number&quot; outlined label=&quot;number&quot;  mask=&quot;##########&quot; minLength=&quot;10&quot; maxlength=&quot;10&quot; @input=&quot;onInput&quot;/&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { ref } from &#39;vue&#39;

export default {

setup() {
const number = ref(&quot;0000000000&quot;)

}
return {
number
}
}
&lt;/script&gt;

onInput does nothing so far

答案1

得分: 2

你应该使用

mask=&quot;#########&quot; fill-mask=&quot;0&quot; reverse-fill-mask

但它只适用于 maxlength=&quot;x&quot;mask=&quot;#*(x-1) (mask=&quot;#########&quot;),不适用于 mask=&quot;#*x

英文:

You should use

mask=&quot;#########&quot; fill-mask=&quot;0&quot; reverse-fill-mask

But it only works well with maxlength=&quot;x&quot; and mask=&quot;#*(x-1) ( mask=&quot;#########&quot;) and does not work with mask=&quot;#*x

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { createApp, ref } = Vue;

const app = createApp({
  setup () {  
    const number1 = ref(0)
    const number2 = ref(&#39;0000000000&#39;)
    const onInput = (newVal) =&gt; {
      number2.value = (&#39;000000000&#39;+ newVal).slice(-10);
    }
    return { number1, number2, onInput }
  }
})

app.use(Quasar)
app.mount(&#39;#q-app&#39;)

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/quasar@2.11.7/dist/quasar.prod.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;div id=&quot;q-app&quot;&gt;
Standard mask: &lt;br/&gt;
number1: {{number1}}
&lt;q-input class=&quot;input&quot; v-model=&quot;number1&quot; outlined label=&quot;number&quot; mask=&quot;##########&quot; fill-mask=&quot;0&quot; reverse-fill-mask minLength=&quot;10&quot; maxlength=&quot;10&quot; @update:model-value=&quot;onInput&quot;&gt;&lt;/q-input&gt;&lt;br/&gt;
Custom mask: &lt;br/&gt;
number2: {{number2}}&lt;br/&gt;
&lt;q-input class=&quot;input&quot; v-model=&quot;number2&quot; outlined label=&quot;number&quot; minLength=&quot;10&quot;  @update:model-value=&quot;onInput&quot;&gt;&lt;/q-input&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/quasar@2.11.7/dist/quasar.umd.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 2

你可以使用String.prototype.padStart函数在字符串达到特定长度之前添加前导零字符。

只需在每次更改时相应地更新 v-model。

&lt;script setup&gt;
import { ref, watch } from &#39;vue&#39;

const counter = ref(&#39;&#39;)
watch(counter, () =&gt; {
  // 移除前导 0
  counter.value = counter.value.substring(Array.from(counter.value).findIndex(v =&gt; v !== &#39;0&#39;))
  // 如有必要,添加前导 0
  counter.value = counter.value.padStart(8, &#39;0&#39;)
})

&lt;/script&gt;

&lt;template&gt;
  &lt;input v-model=&quot;counter&quot; placeholder=&quot;输入数字&quot;&gt;
&lt;/template&gt;

这里是在线演示

英文:

You can use the function String.prototype.padStart to add leading 0 characters until a certain string lenght.

Just update the v-model accordingly whenever it changes.

&lt;script setup&gt;
import { ref, watch } from &#39;vue&#39;

const counter = ref(&#39;&#39;)
watch(counter, () =&gt; {
  // remove leading 0
  counter.value = counter.value.substring(Array.from(counter.value).findIndex(v =&gt; v !== &#39;0&#39;))
  // add leading 0 if necessary
  counter.value = counter.value.padStart(8, &#39;0&#39;)
})

&lt;/script&gt;

&lt;template&gt;
  &lt;input v-model=&quot;counter&quot; placeholder=&quot;Enter a number&quot;&gt;
&lt;/template&gt;

Here is the live demo

huangapple
  • 本文由 发表于 2023年4月13日 18:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定