vue 随机显示数组的其中一个_Vue.js:多个输入中的v-model数组

I have an input text field with a v-model attached, and every time someone hits the "Add" button, another input text get added to the DOM with the same v-model attached. I thought I'd then get an array of the v-model values, but it only gets the value of the first v-model input:

Add

The html I append to the dom is triggered by the addReference method:

addReference: function(e) {

e.preventDefault();

console.log(this.references);

var inputEl = '';

$('#references').append(inputEl);

}

Is this something Vuejs can't do? Is there a different approach of gathering values from dynamic dom elements with Vuejs?

new Vue({

el: "#app",

data: {

references: "text"

},

methods: {

addReference: function(e) {

e.preventDefault();

console.log(this.references);

var inputEl = '';

$('#references').append(inputEl);

}

}

})

input {

display: block;

margin: 1px;

}

Add

解决方案

You're thinking too DOM, it's a hard as hell habit to break. Vue recommends you approach it data first.

It's kind of hard to tell in your exact situation but I'd probably use a v-for and make an array of finds to push to as I need more.

Here's how I'd set up my instance:

new Vue({

el: '#app',

data: {

finds: []

},

methods: {

addFind: function () {

this.finds.push({ value: '' });

}

}

});

And here's how I'd set up my template:

Finds

New Find

Although, I'd try to use something besides an index for the key.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值