2016-11-05 118 views
3

如何動態更改選擇下拉v模型中的選項?動態更改vuejs中的選擇輸入選項2

我有2個選擇輸入,一個應該根據其他變化。

例如,如果我選擇「水果」選擇顯示水果,如果我選擇「蔬菜」它顯示蔬菜。

回答

5

我不使用Vuejs,但看着文件後:

var TypesArr = { 
 
       Fruit: [{ text: 'Apple', value: 'Apple' }, { text: 'Orange', value: 'Orange' }, { text: 'Mango', value: 'Mango' }], 
 
       Meat: [{ text: 'Steak', value: 'Steak' }, { text: 'Pork', value: 'Pork' }] 
 
       } 
 

 

 
var selectTwo = new Vue({ 
 
    el: '#select2', 
 
    data: { 
 
      selected: TypesArr['Fruit'][0], 
 
      options: TypesArr['Fruit'] 
 
     }, 
 
     methods: { 
 
     update: function (value) 
 
     { 
 
      this.options = TypesArr[value] 
 
     } 
 
     } 
 
}) 
 

 

 
new Vue({ 
 
    el: '#select1', 
 
    data: { 
 
      selected: 'Fruit', 
 
      options: [ { text: 'Fruit', value: 'Fruit' }, { text: 'Meat', value: 'Meat' } ] 
 
     }, 
 
     methods: { 
 
     onChange: function (event) 
 
     { 
 
      selectTwo.update(event.srcElement.value) 
 
     } 
 
     } 
 
})
<script src="https://unpkg.com/vue/dist/vue.js"></script> 
 

 
<select v-on:change="onChange" id="select1"> 
 
    <option v-for="option in options" v-bind:value="option.value"> 
 
    {{ option.text }} 
 
    </option> 
 
</select> 
 

 
<select id="select2"> 
 
    <option v-for="option in options" v-bind:value="option.value"> 
 
    {{ option.text }} 
 
    </option> 
 
</select>

+1

的例子我用這個vue實例https://jsfiddle.net/aj6g87dh/1/ tyring這是爲什麼第二個選擇是否對改變有幫助嗎?選擇是空的 – TheShun

+0

@ TheShun你能設法讓它工作嗎?我也有同樣的問題。 – Mike

+0

@Mike - 你有這樣的感覺嗎?林新vue和具有相同的問題!這個由fuzyCap發佈的帖子似乎並沒有工作,可能可以下載到cdn上的vue.js版本,當你點擊「運行代碼片段」時,它將被拖入。 – Birdy

2

使用純JavaScript

var TypesArr = {Fruit: ['Apple', 'Orange', 'Mango'], Meat: ['Steak', 'Pork']} 
 

 

 
function ChangeContext(value) 
 
{ 
 
    if (TypesArr.hasOwnProperty(value)) 
 
    { 
 
     var out = '' 
 

 
     for (var i = 0; i < TypesArr[value].length; i++) 
 
     { 
 
      out += '<option value="' + TypesArr[value][i] + '">' + TypesArr[value][i] + '</option>' 
 
     } 
 

 
     document.getElementById('select2').innerHTML = out 
 
    } 
 
} 
 

 
ChangeContext('Fruit')
<select onchange="ChangeContext(this.value)"> 
 
    <option value="Fruit">Fruit</option> 
 
    <option value="Meat">Meat</option> 
 
</select> 
 

 
<select id="select2"></select>

+0

看起來很大,這將是理想的,但是這是純JavaScript,我需要一個與Vuejs – TheShun

1

其他答案不是真的'Vue'的答案。

你如何處理這取決於你想用選擇框做什麼。我假設你會處理表單上的輸入。

兩個選項:

  1. 使用計算屬性
  2. 使用v-是否顯示不同的選擇框。如果每個選擇框都有不同的V型

計算屬性

<template> 
<div class="container"> 
    <select id="firstInput" v-model="selected"> 
     <option v-for="option in firstInputOptions" :value="option"> 
     {{ option }} 
     </option> 
    </select> 
    <select id="secondInput" v-model="secondInputSelected"> 
     <option v-for="option in secondInputOptions" :value="option"> 
     {{ option }} 
     </option> 
    </select> 
</div> <!-- /container --> 
</template> 

<script> 
export default { 
    computed: { 
    secondInputOptions(){ 
     return this.selected === 'fruit' ? this.fruit : this.vegetables 
    } 
    }, 
    data() { 
    return { 
     fruit: ['apple', 'banana', 'orange'], 
     vegetables: ['carrot', 'beet', 'celery'], 
     firstInputOptions: ['fruit', 'vegetables'] 
     selected: 'fruit', 
     secondInputSelected: '' 
    } 
    }, 
} 
</script> 

條件呈現

<template> 
<div class="container"> 
    <select id="firstInput" v-model="selected"> 
     <option v-for="option in firstInputOptions" :value="option"> 
     {{ option }} 
     </option> 
    </select> 
    <select id="secondInputFruit" v-model="selected" v-if="selected == 'fruit'"> 
     <option v-for="option in secondInputOptions" :value="option"> 
     {{ option }} 
     </option> 
    </select> 
    <select id="secondInputVegetables" v-model="vegetableSelected" v-else-if="selected == 'vegetables'"> 
     <option v-for="option in secondInputOptions" :value="option"> 
     {{ option }} 
     </option> 
    </select> 
</div> <!-- /container --> 
</template> 

<script> 
export default { 
    data() { 
    return { 
     fruits: ['apple', 'banana', 'orange'], 
     fruitSelected: '', 
     vegetables: ['carrot', 'beet', 'celery'], 
     vegetableSelected: '', 
     firstInputOptions: ['fruit', 'vegetables'] 
     selected: 'fruit' 
    } 
    }, 
} 
</script>