vue js如何设置选择的选项值

Vij*_*jay 10 javascript vue.js

我正在使用vue js为我的应用程序选择输入..我需要设置默认值应该在下拉中选择,而在更改时我想调用两个函数..

我是vue js的新手..

我的代码:

var listingVue = new Vue({

el: '#mountain',

data:{
        formVariables: { 
            country_id: '',
            mountain_id: '',
            peak_id: ''
        },
        countrylist:[],
        mountainlist:[],

},         

ready: function() {

    var datas = this.formVariables;
    this.getCountry();     

},

methods: {

    getCountry: function()
        {

            this.$http.get(baseurl+'/api/v1/device/getCountry',function(response)
            {
                this.$set('countrylist',response.result);      

                //alert(jQuery('#country_id').val());              
            });
        },
    getMountain: function(country_id)
        {
            var datas = this.formVariables;
            datas.$set('country_id', jQuery('#country_id').val() );
            postparemeters = {country_id:datas.country_id};
            this.$http.post(baseurl+'/api/v1/site/getMountain',postparemeters,function(response)
            {
                if(response.result)
                    this.$set('mountainlist',response.result);
                else
                    this.$set('mountainlist','');
            });
        },  
Run Code Online (Sandbox Code Playgroud)

});

 <select 
                    class="breadcrumb_mountain_property" 
                    id="country_id" 
                    v-model="formVariables.country_id" 
                    v-on="change:getMountain(formVariables.country_id);">
                <option 
                  v-repeat = "country: countrylist" 
                  value="@{{country.id}}" >
                  @{{country.name}}
                </option>
            </select>
Run Code Online (Sandbox Code Playgroud)

mvm*_*oay 17

使用vue 2,提供的答案将无法正常工作.我遇到了同样的问题,vue文档并没有那么明确<select>.我找到<select>标签正常工作的唯一方法是(在谈论问题时):

<select v-model="formVariables.country_id">
  <option v-for = "country in countrylist" :value="country.id" >{{country.name}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我认为,@ -sign in @{{...}}是由于刀片,不使用刀片时不应该这样做.


Mah*_*low 10

在VueJS 2中,您可以绑定selected到所需的默认值.例如:

 <select 
   class="breadcrumb_mountain_property" 
   id="country_id" 
   v-model="formVariables.country_id" 
   v-on:change="getMountain(formVariables.country_id);">
   <option 
       v-for = "country in countrylist"
       :selected="country.id == 1"
       :value="country.id" >
       {{country.name}}
   </option>
 </select>
Run Code Online (Sandbox Code Playgroud)

因此,countryList,该国的迭代id为1时将被选中,因为country.id == 1true意味着selected="true".

更新:正如迈克建议的那样,而不是v-on="change:getMountain(formVariables.country_id);"有一种新的方式来绑定事件.还有一个简短的表格@change="getMountain(formVariables.country_id);"


Dou*_*sar 3

您应该使用 ' options' 属性来代替尝试 v-repeat <option></option>

虚拟机

data: {    
  countryList: [
    { text:'United States',value:'US' },
    { text:'Canada',value:'CA' }
  ]
},

watch: {
  'formVariables.country_id': function() {
    // do any number of things on 'change'
   }
}
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

<select 
  class="breadcrumb_mountain_property" 
  id="country_id" 
  v-model="formVariables.country_id" 
  options="countryList">
</select>
Run Code Online (Sandbox Code Playgroud)