0

我试图id在使用v-select. 现在,我很难获得id将被选中的值。有人可以帮我解决这个问题吗?

我的index.html文件是 -

<!DOCTYPE html>
<html>
<head>
<title>VueJs | Select2</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
</head>
<body>
  <div id="pageContent">
    <form method="POST" @submit.prevent="addSection()">
      <div class="form-group">
        <label for="className">Select Class</label>
        <v-select name="className" v-model="className" :options="academicClasses.map(({cname}) => cname)"></v-select>
      </div>
       <div class="form-group">
         <button type="submit" class="btn btn-success save-btn"><i class="fa fa-check"></i> Save</button>
       </div>
    </form>
</div>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-select@latest"></script>

</body>
</html>

我的 vujs 脚本是 -

Vue.component('v-select', VueSelect.VueSelect);

var app = new Vue({
  el: '#pageContent',
  data: {
    className: '',
    academicClasses: [],
    academicClasses: [
            {
            id: 1,
          cname: 'One'
        },
            {
            id: 2,
          cname: 'Two'
        },
            {
            id: 3,
          cname: 'Three'
        },
            {
            id: 4,
          cname: 'Four'
        }
    ],
  },

  methods: {
    addSection(){
        alert(this.className);
    }
  }
})

请您访问JSFiddle我要解释的内容!请访问 - JsFiddle

4

2 回答 2

1

在这里更改:

<v-select name="className" v-model="className" :options="academicClasses.map(academicClass => ({label: academicClass.cname, value: academicClass.id}))"></v-select>

和这里:

alert(this.className.label + ' - ' + this.className.value);

文档:下拉选项

于 2019-02-01T13:10:58.123 回答
1

您想options在以下结构中为道具传递一个对象数组:

[{
  label: item.cname,
  value: item.id
}]

Vue.component('v-select', VueSelect.VueSelect);

var app = new Vue({
  el: '#pageContent',
  
  data() {
    return {
      className: '',
      
      academicClasses: [
        { id: 1, cname: 'One'}, 
        { id: 2, cname: 'Two' }, 
        { id: 3, cname: 'Three' }, 
        { id: 4, cname: 'Four' }
      ],
    }
  },
  
  methods: {
    addSection() {
      alert(this.className);
    }
  },
  
  computed: {
    opts() {
      return this.academicClasses.map(item => ({
        label: item.cname,
        value: item.id
      }));
    }
  },

  watch: {
    className(val) {
      console.log(val);
    }
  }
})

Vue.config.devtools = false;
Vue.config.productionTip = false;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-select@latest"></script>

<div id="pageContent">
  <form method="POST" @submit.prevent="addSection()">
    <div class="form-group">
      <label for="className">Select Class</label>
      <v-select name="className" v-model="className" :options="opts"></v-select>
    </div>
    <div class="form-group">
      <button type="submit" class="btn btn-success save-btn"><i class="fa fa-check"></i> Save</button>
    </div>
  </form>
</div>

于 2019-02-01T13:14:46.080 回答