Vue中select的使用

效果:

HTML:

<div class="sel01">
    <select v-model="selectClassEnd" @change="selectClass($event)">
        <option value="NONE">未选择</option>
        <option v-for="(options,id) in selectClassData" :key="id" :value="options.id">
            {{options.title}}
        </option>
    </select>
</div>

CSS:

.sel01{display:inline-block;position:relative;z-index:2;font-size:1.6rem;height:3.6rem;line-height:3.6rem;width:8rem;flex:1;background:#fff;box-sizing:border-box;border-radius:.5rem;}
.sel01:before{content:"";position:absolute;width:0;height:0;border:.5rem solid transparent;
border-top-color:#e92f26;top:50%;right:1rem;cursor:pointer;z-index:-2;margin-top:-0.25rem;}
.sel01 select{width:100%;border:none;height:3.6rem;background:transparent;background-image:none;-webkit-appearance:none;-moz-appearance:none;vertical-align:top;padding-left:1rem;}
.sel01 select:focus{outline:none;}

JS:

export default {
    name:"Name",
    data(){
      return{
        selectClassData:[ //类别选择数据或者从后台获取数据
          {id:1,title:"科普类"},
          {id:2,title:"亲子类"},
          {id:3,title:"制作类"},
          {id:4,title:"创意类"}
        ],
        selectClassEnd:"NONE",//类别默认选择
        select_class_id:"",//类别id提交报名需要
      }
    },
    methods:{
     //类别选中
     selectClass(event){
      this.select_class_id = event.target.value; //获取option对应的value值 select_class_id是后台约定的提交数据的名称
     },
    }

}

  • 15
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
v-model实现动态绑定 Vueselect下拉框可以通过v-model来动态绑定选的值。如下所示: ``` <template> <select v-model="selected"> <option disabled value="">请选择</option> <option v-for="(item,index) in options" :key="index" :value="item.value">{{item.label}}</option> </select> </template> <script> export default { data() { return { selected: '', options: [ { label: '选项1', value: '1' }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' } ] }; } }; </script> ``` 在上面的代码,通过v-model来绑定select下拉框的选值,options数组存放了选项的内容和值,通过v-for来遍历数组,将选项渲染到下拉框。 通过computed计算属性实现动态渲染 有时候,我们需要根据某些条件动态渲染下拉框的选项。这时,可以通过computed计算属性来实现。如下所示: ``` <template> <select v-model="selected"> <option disabled value="">请选择</option> <option v-for="(item,index) in options" :key="index" :value="item.value">{{item.label}}</option> </select> </template> <script> export default { data() { return { selected: '', options: [ { label: '选项1', value: '1' }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' } ], showOption3: false }; }, computed: { computedOptions() { if (this.showOption3) { return this.options; } else { return this.options.filter((item) => item.value !== '3'); } } } }; </script> ``` 在上面的代码,通过computed计算属性来动态渲染选项。computedOptions方法根据showOption3的值来判断是否渲染选项3,返回的数组会重新渲染下拉框的选项。 通过watch监听选值的变化 当选的值发生变化时,我们可能需要触发某些事件或者更新其他组件的状态。这时可以通过watch来监听选值的变化。如下所示: ``` <template> <select v-model="selected"> <option disabled value="">请选择</option> <option v-for="(item,index) in options" :key="index" :value="item.value">{{item.label}}</option> </select> </template> <script> export default { data() { return { selected: '', options: [ { label: '选项1', value: '1' }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' } ] }; }, watch: { selected(val) { console.log(val); } } }; </script> ``` 在上面的代码,通过watch来监听选值的变化,当选值发生变化时,会触发selected方法,并打印出选的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值