[英]How to create dynamic onclick elements in vuejs without repeating same value
I am trying to create a form in vuejs, where a group of inputs can be append onclick.我正在尝试在 vuejs 中创建一个表单,其中一组输入可以附加 onclick。 It works fine, but the problem is, All inputs return the same value.
它工作正常,但问题是,所有输入都返回相同的值。 I am sharing an image here :
我在这里分享一张图片:
I am sharing my code from template :我从模板分享我的代码:
<div class="form-group" v-for="(input,k) in data.invoice_product" :key="k">
<div class="row mb-2">
<div class="col-md-3">
<select class="form-control" v-model="data.invoice_product.product_id"
@change="getProductCost">
<option v-for="(product, i) in products" :key="i" :value="product.id">{{
product.product_name }}</option>
</select>
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder="Quantity" v-
model="data.invoice_product.quantity" @keyup="getProductCost">
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder="Total" v-
model="data.invoice_product.total">
</div>
<div class="col-md-3">
<span>
<i class="fa fa-minus-circle" @click="removeElement(k)" v-show="k || ( !k
&& data.invoice_product.length > 1)">Remove</i>
<i class="fa fa-plus-circle" @click="addElement(k)" v-show="k ==
data.invoice_product.length-1">Add fields</i>
</span>
</div>
</div>
</div>
from my script (I am excluding irrelevant code segments) :从我的脚本中(我排除了不相关的代码段):
export default {
data() {
return {
data : {
customer_id : '',
vat : ''
},
inputs: [{
product_id : '',
quantity : '',
total : ''
}],
input: {
product_id : '',
quantity : '',
total : ''
},
products : []
}
},
methods : {
getProductCost() {
axios.get('/api/product-cost?
product_id='+this.item.product_id+'&&quantity='+this.item.quantity,
this.data).then(response => {
this.input.total = response.data
})
},
addElement() {
this.data.invoice_product.push({
product_id : '',
quantity : '',
total : ''
})
},
removeElement (index) {
this.data.invoice_product.splice(index, 1)
},
}
Input returns null if I use "input" instead :如果我改用“输入”,则输入返回 null:
The problem is not providing correct data to v-model.问题在于没有向 v-model 提供正确的数据。
Here, you make an iteration, where you get "input" as an element.在这里,您进行了一次迭代,在这里您将“输入”作为一个元素。
<div class="form-group" v-for="(input,k) in data.invoice_product" :key="k">
But here, you are providing "data.invoice_product" instead of "input".但在这里,您提供的是“data.invoice_product”而不是“input”。
<select class="form-control" v-model="data.invoice_product.product_id"
@change="getProductCost">
Just change "data.invoice_product.product_id" to "input.product_id", and also do it for other inputs.只需将“data.invoice_product.product_id”更改为“input.product_id”,其他输入也可以这样做。
You are already looping through data.invoice_product
with this您已经使用此循环遍历
data.invoice_product
<div class="form-group" v-for="(input,k) in data.invoice_product"> .... </div>
so the v-model
on your select
tag should be所以你的
select
标签上的v-model
应该是
<select v-model="input.product_id"> .... </select>
instead of代替
<select v-model="data.invoice_product.product_id"> .... </select>
Similar case for your input
tags for Quantity
and Total
. Quantity
和Total
的input
标签的类似情况。
So, the code in your template should be something like this:因此,模板中的代码应该是这样的:
<div class="form-group" v-for="(input,k) in data.invoice_product" :key="k">
<div class="row mb-2">
<div class="col-md-3">
<select class="form-control" v-model="input.product_id"
@change="getProductCost">
<option v-for="(product, i) in products" :key="i" :value="product.id">{{
product.product_name }}</option>
</select>
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder="Quantity" v-
model="input.quantity" @keyup="getProductCost">
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder="Total" v-
model="input.total">
</div>
<div class="col-md-3">
<span>
<i class="fa fa-minus-circle" @click="removeElement(k)" v-show="k || ( !k
&& data.invoice_product.length > 1)">Remove</i>
<i class="fa fa-plus-circle" @click="addElement(k)" v-show="k ==
data.invoice_product.length-1">Add fields</i>
</span>
</div>
</div>
</div>
[Updated] [更新]
Your scripts should be left as it was before:您的脚本应该保持原样:
export default {
data() {
return {
data : {
customer_id : '',
vat : '',
invoice_product: [{
product_id : '',
quantity : '',
total : ''
}],
},
input: {
product_id : '',
quantity : '',
total : ''
},
products : []
}
},
methods : {
addElement() {
this.data.invoice_product.push(this.input)
},
removeElement (index) {
this.data.invoice_product.splice(index, 1)
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.