我用的是Vue-多重选择和Laravel。
我正在使用表单中的multiselect组件来让用户选择多个国家。该组件工作良好,但当我提交表单并对其进行dd()时,它将显示[object Object]。
我无法得到多选择组件的值。我发现了类似的问题,但没有一个对我有用。
这是我的代码:
ExampleComponent.vue文件:
<template slot-scope="{ option }">
<div>
<label class="typo__label">Restricted country</label>
<multiselect
v-model="internalValue"
tag-placeholder="Add restricted country"
placeholder="Search or add a country"
label="name"
name="selectedcountries[]"
:options="options"
:multiple="true"
track-by="name"
:taggable="true"
@tag="addTag"
>
</multiselect>
<pre class="language-json"><code>{{ internalValue }}</code></pre>
</div>
</template>
<script>
import Multiselect from 'vue-multiselect'
// register globally
Vue.component('multiselect', Multiselect)
export default {
components: {
Multiselect
},
props: ['value'],
data () {
return {
internalValue: this.value,
options: [
{ name: 'Hungary' },
{ name: 'USA' },
{ name: 'China' }
]
}
},
watch: {
internalValue(v){
this.$emit('input', v);
}
},
methods: {
addTag (newTag) {
const tag = {
name: newTag,
code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000))
}
this.options.push(tag)
this.value.push(tag)
}
},
}
</script>这是我的登记表:
<div id="select">
<example-component v-model="selectedValue"></example-component>
<input type="hidden" name="countriespost" :value="selectedValue">
</div>
<script>
const select = new Vue({
el: '#select',
data: {
selectedValue: null
},
});
</script>当我提交表单时,countriespost向我显示如下:[object Object]而不是实际值。
发布于 2018-08-14 18:30:18
这是因为您提供了一个对象数组作为options属性:
options: [
{ name: 'Hungary' },
{ name: 'USA' },
{ name: 'China' }
]因此,在input上发出的值是一个对象。尝试将选项更改为:
options: [ 'Hungary', 'USA', 'China' ]发布于 2018-08-14 18:35:12
如果将一个对象数组传递给multiselect组件的:options支柱,则应该提交带有javascript的表单,以便提取后端需要的对象If或任何东西,然后将它们发送出去。添加如下方法:
submit: function() {
let data = {
objectIds: _.map(this.selectedOptions, option => option.id), //lodash library used here
// whatever other data you need
}
axios.post('/form-submit-url', data).then(r => {
console.log(r);
});
}然后通过提交按钮上的@click.stop事件触发它。
https://stackoverflow.com/questions/51846897
复制相似问题