简体   繁体   English

如何从多个 vue select 实例中获取值?

[英]How do I get values from multiple vue select instances?

I have 2 arrays of objects like below.我有 2 个对象数组,如下所示。 I need to create a select for every questions element, and I need to have the options connected with the selections by the id.我需要为每个questions元素创建一个选择,并且我需要通过 id 将选项与selections联。 In this case I need to have 2 selects, the first one will have 1000 , 5000 and 10000 as options meanwhile the second select will have yes and no as options在这种情况下,我需要有 2 个选择,第一个将有1000500010000作为选项,而第二个选择将有yesno作为选项

const questions = [{
        'id': 1,
        'question': 'KM'
    },
    {
        'id': 2,
        'question': 'Works'
    }
]

const selections = [{
        'question_id': 1,
        'value': 1000
    },
    {
        'question_id': 1,
        'value': 5000
    },
    {
        'question_id': 1,
        'value': 10000
    },
    {
        'question_id': 2,
        'value': 'yes'
    },
    {
        'question_id': 2,
        'value': 'no'
    }
]

I made it like this in vue but the issue is that I can't save the data in the v-model as I'm returning the values from cars() and not a variable specified in data()我在 vue 中做到了这一点,但问题是我无法将数据保存在 v-model 中,因为我正在返回来自cars()的值而不是data()指定的变量

<div class="form-group" v-for="item in questions" v-bind:key="item.question">
    <h5 v-if="showQuestion(item.id)">{{ item.question }}</h5>

    <div class="tour-options-select" v-if="showQuestion(item.id)">
        <select id="select-suggestions" name="tour-options-dropdown" class="tour-options-dropdown" v-model="questions.value">
            <option v-for="item1 in cars(item.id)" v-bind:key="item1.id" :value="item1.display_name">{{ item1.display_name }}</option>
        </select>
    </div>
</div>

Ultimately, I just need to know, how do I get the value when I have a structure like the one I defined above?最终,我只需要知道,当我有一个像我上面定义的结构时,我如何获得价值?

If you have an array in data() to store your selected options, you can use v-model to dynamically bind with an element in that array if you give it an index:如果你在data()有一个数组来存储你选择的选项,你可以使用v-model动态绑定该数组中的元素,如果你给它一个索引:

 new Vue({ el: '#app', data: { questions: [{ 'id': 1, 'question': 'KM' }, { 'id': 2, 'question': 'Works' } ], selections: [{ 'question_id': 1, 'value': 1000 }, { 'question_id': 1, 'value': 5000 }, { 'question_id': 1, 'value': 10000 }, { 'question_id': 2, 'value': 'yes' }, { 'question_id': 2, 'value': 'no' } ], selected: [], }, methods: { cars: function(id) { return this.selections.reduce((arr, currSel) => { if (currSel.question_id == id) arr.push(currSel); return arr; }, []); }, } });
 <script src="https://unpkg.com/vue@2.6.12/dist/vue.min.js"></script> <div id="app"> <div v-for="(question, index) in questions" :name="question.question" :key="question.id"> <select v-model="selected[index]"> <option v-for="option in cars(question.id)" :key="option.question_id" :value="option.value"> {{ option.value }} </option> </select> </div> <p>Selected:</p> <pre>{{ $data.selected }}</pre> </div>

Another approach would be to use events to handle the changes, calling a custom function each time the user makes a selection, eg using @change :另一种方法是使用事件来处理更改,每次用户进行选择时调用自定义函数,例如使用@change

 new Vue({ el: '#app', data: { questions: [{ 'id': 1, 'question': 'KM' }, { 'id': 2, 'question': 'Works' } ], selections: [{ 'question_id': 1, 'value': 1000 }, { 'question_id': 1, 'value': 5000 }, { 'question_id': 1, 'value': 10000 }, { 'question_id': 2, 'value': 'yes' }, { 'question_id': 2, 'value': 'no' } ], }, methods: { cars: function(id) { return this.selections.reduce((arr, currSel) => { if (currSel.question_id == id) arr.push(currSel); return arr; }, []); }, } });
 <script src="https://unpkg.com/vue@2.6.12/dist/vue.min.js"></script> <div id="app"> <div v-for="(question, index) in questions" :name="question.question" :key="question.id"> <select @change="console.log('Option', $event.target.value, 'chosen for Q', question.id)"> <option selected disabled>Select...</option> <option v-for="option in cars(question.id)" :key="option.question_id" :value="option.value"> {{ option.value }} </option> </select> </div> </div>

This way will give you more freedom to store or process the data as you wish, but you'll have to do it manually.这种方式将使您更自由地根据需要存储或处理数据,但您必须手动进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM