Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

select 下拉框绑定值问题

  data() {
    return {
      value1: "",
      type: [{id: 1, name: '肖明'},{id: 2, name: '小红'},{id: 3, name: '小光'}],
      list: [1,2,3,4,5]
    }
  }
  
 <div v-for="(item, index) in list">
  <Select v-model="value1" @on-change="change">
    <Option
      v-for="_item in type"
      :value="_item.id"
      :key="_item.id"
    >{{ _item.name }}</Option>
  </Select>
 </div>

通过循环生成了多个 select框 绑定的都是同一个下拉数据源, v-model 绑定的 value1在data中有定义声明,但是list的length 是不确定的,所以每个select的v-model不可能在data中声明....
想请教下,这种情况下怎么去绑定循环生成的select的值,在改变一个select的值时其他的select不会跟着改变....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你的select会循环生成多个,那你绑定的value肯定也是对应的多个啊?为什么是多个下拉绑定一个value呢?这样肯定会出现改变一个,其他的都会跟着改变呀。

你的value可以根据你的list长度,来生成对应个数的value

// value写成计算属性
computed: {
    value() {
        var valueArr = this.list.map(item => ({value: ''}))
        return valueArr
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...