使用Vue JS复制输入字段,增加索引
aUsing Vue JS to duplicate an input field, incrementing the index
我需要复制一个文本输入字段,当用户单击按钮"添加另一个",并增加新字段上的索引。它和另一个问题很相似但是解没有增加任何东西。
复制字段并增加索引,但它影响所有索引,而不仅仅是最新的索引。(感谢Roy J的提示)
这是我的模板:
<div id="app">
<template v-for="slot in timeslots">
<div><input type="text" name="performances[@{{ count }}][timestamp]" v-model="slot.timestamp" placeholder="index @{{ count }}"></div>
</template>
<span class="add green btn" @click="addAnother"><i class="fa fa-plus-circle"></i> Add another</span>
<pre>@{{ timeslots | json }}</pre>
</div>
这是我在Vue JS中的内容:
new Vue({
el: '#app',
data: {
timeslots: [
{
timestamp: '',
count: 0
}
],
count: 0
},
methods: {
addAnother: function(){
this.timeslots.push({
timestamp: '',
count: this.count++
});
}
}
});
它适用于我,如果我只是合格的count++
与this
。我将其设置为预增量,以避免使第一个元素重复。
我已经更改了占位符文本,以引用slot.count
(当前的count
,而不是父count
)。
new Vue({
el: '#app',
data: {
timeslots: [{
timestamp: '',
count: 0
}],
count: 0
},
methods: {
addAnother: function() {
this.timeslots.push({
timestamp: '',
count: ++this.count
});
}
}
});
.green.btn {
background-color: green;
color: white;
padding: 5px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<div id="app">
<template v-for="slot in timeslots">
<div>
<input type="text" name="performances[@{{ slot.count }}][timestamp]" v-model="slot.timestamp" placeholder="index {{ slot.count }}">
</div>
</template>
<span class="add green btn" @click="addAnother"><i class="fa fa-plus-circle"></i> Add another</span>
<pre>@{{ timeslots | json }}</pre>
</div>
相关文章:
- 名称输入的索引
- JQuery使计数器每次更改时都会增加
- 在jQuery中获取表的行索引
- 测试索引值是否等于某个数字的倍数
- 循环遍历数组中的特定索引
- 按照选项卡索引的顺序循环一个jQuery选择
- 在JavaScript中通过索引从对象数组中获取值
- 如何增加同时按键总数
- 如何使用css动画从中心增加边界线
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 在索引.html和应用.js [node.js] 之间共享变量
- 如何为高图中的区域线创建z索引
- 下拉列表在使用z索引放置在前面后停止工作
- 减去两个索引不同但值相同的整数
- JavaScript:单击时相对于父级增加变量值
- 在使用 javascript 动态添加行时,我正在增加索引值,但出现错误
- 如何在视频导航上增加视频索引
- 为什么数组中的字符串索引不会增加'长度'
- 使用Vue JS复制输入字段,增加索引
- 如何使用jQuery增加索引值