【发布时间】:2019-07-23 15:32:10
【问题描述】:
我有一个带有 vuejs 的表单,并希望在提交来自输入字段的数据时进行连接。我附上了一个jsfiddle,在那里我试图解决concat但没有成功。
我为三个输入字段设置了三个 vue vomponents,其中第一个是下拉列表,其中自定义选项应更改为输入文本字段。
如果我试图连接三个数据,js 会给我未定义的错误。
new Vue({
el: '#product_id',
data: {
selected: '1',
options: [
{ text: 'Product 1', id: '1', value: '1' },
{ text: 'Product 2', id: '2', value: '2' },
{ text: 'Product 3', id: '3', value: '3' },
{ text: 'Product 4', id: '4', value: '4' },
{ text: 'Custom', id: '5', value: '' }
],
product_i: '',
resetKey: 0,
},
methods:{
updateComponent(){
this.resetKey += 1;
console.log(this.resetKey);
console.log('test');
}
},
}),
new Vue({
el: "#product_name",
data: {
product_n: '',
}
});
new Vue({
el: "#product_price",
data: {
product_p: '',
}
});
function combine_product_datas() {
var id = document.getElementById('input1').value;
var name = document.getElementById('input2').value;
var price = document.getElementById('input3').value;
document.getElementById('joint').value = id + '.' + name + '/' + price;
alert(document.getElementById('joint').value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="row">
<div id="product_id">
<div :key="resetKey">
<small>Product id</small>
<div v-if="selected != ''">
<select v-model="selected">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
</div>
<br>
<div v-else>
<input id="id" v-model="product_i" value="" type="text" placeholder="Add your product">
<button v-on:click="updateComponent">Reset</button>
</div>
</div>
</div>
<br>
<div id="product_name">
<div>
<div>
<small>Product name</small><br>
<input id="name" v-model="product_n" type="text">
</div>
</div>
</div>
<br>
<div id="product_price">
<div>
<div>
<small>Product price</small><br>
<input id="price" v-model="product_p" type="text">
</div>
</div>
</div>
<br>
<div>
<div>
<button type="submit" onclick="combine_product_datas();">Combine datas</button>
</div>
</div>
<br>
<input type="hidden" id="joint">
<br>
</div>
【问题讨论】:
-
请添加导致错误的代码
-
我尝试使用的代码在之前添加的 jsfiddle 链接上。
-
嘿 Tikinu,我看看你的代码,如果你只是想让你的 v-if / v-else 逻辑工作,只需删除:
标签(来自 HTML 代码)和逻辑将工作:) 当您使用 v-if 和 v-else-if 或 v-else 时,您不能在这些代码之间放置任何代码,好吗?希望有帮助;) -
谢谢朱利亚诺,确实我删除了 br 标签,这部分工作。串联怎么办?或者,如果我从列表中选择自定义选项并出现输入字段,您能否帮助下拉菜单,我想使用重置按钮重新呈现下拉组件。有可能吗?
标签: javascript vue.js