LoginSignup
0
0

More than 1 year has passed since last update.

Vueで<select>タグを使う時に、選択されているvalue属性をスクリプトで決定する方法について。

Last updated at Posted at 2019-09-18

Vue
v-modelを使います。

概要:
select 要素v-modelにした変数に対して、option 要素に設定されたvalueと同じ値を代入すれば良い。

html
<select v-model="selectNumber" v-on:change="selectIndex">
    <option v-for="item in valueList" v-bind:value="item.number">{{item.title}}</option>
</select>
vue_component
var vueComponent = {
  data: {
    //
    // @note: v-modelに束縛、ここで選択状態の初期化も行う。
    //
    selectNumber: 1,

    //
    // @note: 選択項目にするデータ
    //
    valueList: [{title:"one", number:1},{title:"two", number:2}],
  },

  method: {
    //
    // @note: ここでスクリプト側から select 要素の選択状態を設定する。
    //
    setSelectNumber: function(idx){
      // selectのv-modelで束縛した変数に value属性の値を代入すれば実際に変更される。
      this.selectNumber = valueList[idx].number;
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0