如何在 Vue 中将格式化的输入文本存储到带有索引的数组中?
How to store formatted input text into an array with indexes in Vue?
这是这个问题的扩展
我对如何从我的文本输入到数组列表中获取格式化值列表感到困惑。
我需要在矩阵中执行此操作,但将其简化为数组。
请帮忙,谢谢!
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div
v-for="(input, index) in valueInputs" <-- index
:key="index"
>
<input
v-model="value" // <-- I want to track what index I'm in
@input="formatTime" // <-- so I can set it in an array later
maxLength="4" // I tried formatTime[index] or value[index]
id="format-value" // but that doesn't work, how to pass index
class="input" // into formatTime or value fields?
type="text"
/>
</div>
</div>
data () {
return {
valueInputs: [], // a list of inputs
allFormatValues: [] // want to store all the formatted values here by the index
}
}
想要设置一个存储所有格式化值的数组:
this.allFormatValues[index] = this.value;
我不确定如何将索引与格式化字符串值相关联?
您正在检索 valueInputs
数组的值而不是它的索引。但是,您可以通过以下方式获取v-for
中每个值的索引:
v-for="(value, index) in valueInputs"
这是这个问题的扩展
我对如何从我的文本输入到数组列表中获取格式化值列表感到困惑。 我需要在矩阵中执行此操作,但将其简化为数组。
请帮忙,谢谢!
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div
v-for="(input, index) in valueInputs" <-- index
:key="index"
>
<input
v-model="value" // <-- I want to track what index I'm in
@input="formatTime" // <-- so I can set it in an array later
maxLength="4" // I tried formatTime[index] or value[index]
id="format-value" // but that doesn't work, how to pass index
class="input" // into formatTime or value fields?
type="text"
/>
</div>
</div>
data () {
return {
valueInputs: [], // a list of inputs
allFormatValues: [] // want to store all the formatted values here by the index
}
}
想要设置一个存储所有格式化值的数组:
this.allFormatValues[index] = this.value;
我不确定如何将索引与格式化字符串值相关联?
您正在检索 valueInputs
数组的值而不是它的索引。但是,您可以通过以下方式获取v-for
中每个值的索引:
v-for="(value, index) in valueInputs"