当前位置: 首页 > 网络编程 > JavaScript

vue操作select获取option值方式

时间:2025-03-19 10:37:05 JavaScript 我要投稿
文章介绍了在Vue中操作select元素并实时获取选中的option值的方法,通过使用`@change`事件并传递参数,可以实现动态获取选中的值

vue操作select获取option值

如何实时的获取你选中的值 只用@change件事

@change="changeCategoryList($event)" 动态传递参数

vue操作select获取option的value值

示例

  <div id="app">
        <select name="topicCategoryId" class="article-title" id="category" @change="changeCategoryList($event)"  style="width: 180px;">
                <option  v-for="(category,index) in categoryList" v-bind:value="category.id">{{category.categoryTitle}}</option>
         </select>
   </div
  data:{
        categoryList:[{id:1,categoryTitle:"Java面试"},{id:2,categoryTitle:"Web开发"},
        {id:3,categoryTitle:"设计系统"},{id:4,categoryTitle:"框架服务"}]
     },
                
  methods: {
         changeCategoryList(event){
            console.log(event);
            var selectIndex =event.target.selectedIndex;//选中的第几个option元素 从0开始
            var options = event.target.options;//代表option组合
            
            options[selectIndex].innerHTML  //选择的值 // categoryTitle
            event.target.value;  //绑定的value值  //v-bind:value="category.id"
                                   
            console.log("options[selectIndex].innerHTML=="+options[selectIndex].innerHTML);
            console.log("event.target.value=="+event.target.value);
        }
       }

vue操作select获取option值方式(图1)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。