【发布时间】:2021-08-06 13:40:30
【问题描述】:
我有多个文本框,当用户在不同的文本框中键入时,我想要一个数组来存储所有这些格式化数据。
格式化数据的单位是 m:ss (m - 分钟, s - 秒)
现在,所有不同的文本框都显示相同的值,因为只有一个 this.formatTime。
我们如何改变这一点,以便 v-model 遍历数组,并将其添加到格式化值数组中?
文本框应显示格式化值并将其存储在 allFormatValues[] 中。
我真的很困惑,谢谢你的时间!
<div id="app">
<div
v-for="(input, index) in valueInputs"
:key="index"
>
<input
v-model="formatTime" //want to use allFormatValues[index], instead
id="time-input" //but this loses the formatting if I change to
//above
type="text"
/>
</div>
</div>
和
watch: {
formatTime () {
const totalLength = this.formatTime.length;
let a = this.formatTime.replace(/[^0-9]/g, "")
.substr(0, 1);
const b = this.formatTime.replace(/[^0-5]/g, "")
.substr(1, 1);
const c = this.formatTime.replace(/[^0-9]/g, "")
.substr(2, 1);
if (totalLength >= 2) {
a = `${a.substring(0, 1)}:${b}${c}`;
}
this.formatTime = a;
},
}
我有一个数组我想循环并设置这个值
data () {
return {
valueInputs: [], // a list of inputs
allFormatValues: [] // want to store all the formatted values here by the index
}
}
【问题讨论】:
标签: javascript vue.js vuejs2 watch v-model