Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀
vue select下拉框的默認(rèn)選中項(xiàng)的三種情況
在Vue中 使用select下拉框 主要靠的是 v-model 來綁定選項(xiàng) option 的 value 值。
select下拉框在界面的展示,我們都希望看到框中有一個(gè)值 而不是空白,比如顯示 “請選擇” 或者 “全部” 的默認(rèn)值。
先來看效果圖
關(guān)于select選項(xiàng)的寫法 有三種情況
- ①.寫在HTML中
- ②.寫在JS一個(gè)數(shù)組中
- ③.通過接口去獲取得到
我們直接上代碼:
第一種是option的值寫在HTML中
<div id="app"> <select name="status" id="status" v-model="selected"> <option value="">請選擇</option> <option value="1">未處理</option> <option value="2">處理中</option> <option value="3">處理完成</option> </select> </div> <script> new Vue({ el:'#app', data:{ selected:'' //默認(rèn)選中項(xiàng)的value值是什么 就給綁定的屬性什么值 這里默認(rèn)選中項(xiàng)請選擇的value值是空 我們就給綁定的屬性 select 一個(gè)空值 }, }) </script>
第二種是option 選項(xiàng)內(nèi)容寫在JS中的
通過v-for來遍歷的:
<body> <div id="app"> <select name="status" id="status" v-model="selected"> <option :value="item.statusId" v-for="(item,index) in statusArr">{{item.statusVal}}</option> </select> </div> </body> <script> new Vue({ el:'#app', data:{ statusArr:[ { statusId:'0', statusVal:'請選擇' }, { statusId:'1', statusVal:'未處理' }, { statusId:'2', statusVal:'處理中' }, { statusId:'3', statusVal:'處理完成' }, ], selected:'' }, created(){ // 在組件創(chuàng)建之后,把默認(rèn)選中項(xiàng)的value值賦給綁定的屬性 //如果沒有這句代碼,select中初始化會是空白的,默認(rèn)選中就無法實(shí)現(xiàn) this.selected = this.statusArr[0].statusId; } }) </script>
第三種是option 選項(xiàng)內(nèi)容
通過接口去獲取 但是接口里沒有默認(rèn)選中項(xiàng)怎么辦呢?看代碼
<body> <div id="app"> <select name="status" id="status" v-model="selected"> <!-- 由于從接口獲取的select的下拉值沒有‘請選擇',所以我們要自己寫一個(gè) --> <option value="">請選擇</option> <option :value="item.statusId" v-for="(item,index) in statusArr">{{item.statusVal}}</option> </select> </div> </body> <script> new Vue({ el:'#app', data:{ statusArr:[], //用來接收從接口里獲取出來的select下拉框里的值 selected:'' }, getSelectInfo(){ var url = "../monitor_admin_front/device/status"; //接口地址 axios.get(url) .then(response => { if(response.data.retCode == 0){ this.statusArr = response.data.data; //將獲取出來的數(shù)據(jù)賦給定義的數(shù)組 以便于去循環(huán)遍歷 } }) }, created(){ this.getSelectInfo(); } }) </script>
vue中select默認(rèn)選中下拉選項(xiàng)第一條(舉例iview AutoComplete組件)
html中
- 靜態(tài)
給對應(yīng)option元素直接添加selected屬性
<select> <option value="0">one</option> <option value="1">two</option> <option value="2" selected>three</option> </select>
- 動(dòng)態(tài)
option加載完成后給對應(yīng)元素添加selected屬性
$("option")[2].prop("selected","selected");
vue中
選中第一項(xiàng)同時(shí)并改變select綁定的value(正常業(yè)務(wù))
<template> <select v-model="value"> <option value="0">New York</option> <option value="1">London</option> <option value="2">Sydney</option> <option value="3">Ottawa</option> </select> </template> export default { data () { return { value: '0' } } }
可模糊匹配,select輸入模糊匹配,默認(rèn)選中下拉選項(xiàng)中第一項(xiàng),但select綁定的value不變,即下圖所示:
我的做法是
1.輸入值改變時(shí),監(jiān)聽change事件引起下拉選項(xiàng)的發(fā)生改變
2.判斷篩選后的下拉選項(xiàng)Dom元素中沒有選中項(xiàng),為第一項(xiàng)New York添加選中樣式;若有選中項(xiàng)則不操作
3.監(jiān)聽回車事件,如此時(shí)有回車事件,判斷下拉選項(xiàng)Dom中第一項(xiàng)有選中樣式則將value的值變更為選項(xiàng)中第一項(xiàng)的值,即this.value = ‘New York’,并將下拉選項(xiàng)框隱藏
4.我的實(shí)際列子是使用iviewUI中的AutoComplete組件做的(簡單舉個(gè)例子,只提供參考)
<template> <div id="search-input-component"> <AutoComplete v-model="value" type="text" @keydown.enter.native.prevent="enterEvent" @on-change="change" > <Option v-for="item in changeList" :value="item.value" :key="item.value" > {{item.name}} </Option> </AutoComplete> </div> </template> <script> export default { data () { return { value: '', // AutoComplete綁定的值 valueLists: [ // { value: '選項(xiàng)值', name: '選項(xiàng)名' } ], // 全部拉選項(xiàng)列表 changeList: [] // 實(shí)時(shí)模糊匹配的下拉選項(xiàng)列表 } }, created () { this.getAllList() }, methods: { // 獲取所有下拉選項(xiàng) getAllList () { // ...Data this.valueLists = Data this.changeList = Data }, // 輸入change事件 change (val) { // 模糊匹配邏輯,重新生成下拉選項(xiàng)列表 this.changeList = [] this.valueLists.map(item => { if (item.toUpperCase().indexOf(val.toUpperCase()) !== -1) { this.changeList.push(item) } }) this.$nextTick(() => { // 下拉框中所有選項(xiàng)的Dom const ele = document.querySelectorAll('#search-input-component .ivu-select-dropdown.ivu-auto-complete .ivu-select-dropdown-list .ivu-select-item') let hasFocus = false // 初始賦值沒有選中項(xiàng) for(let i=0; i < ele.length; i++) { // 判斷有選中項(xiàng) if (ele[i].className.indexOf('ivu-select-item-focus') > -1) { hasFocus = true break } } // 判斷沒有選中項(xiàng),則選中第一項(xiàng)(添加選中樣式) if (!hasFocus && (ele[0].className.indexOf('ivu-select-item-focus') === -1)) { ele[0].classList += ' ivu-select-item-focus' } }) }, // 回車事件 enterEvent (card, isUpdate) { const selectDom = document.querySelector('#search-input-component .ivu-select-dropdown.ivu-auto-complete') // 下拉選項(xiàng)有匹配到數(shù)據(jù) 并且 下拉選項(xiàng)沒有隱藏 if (this.changeList.length && !selectDom.style.display) { // 下拉框中所有選項(xiàng)的Dom const items = document.querySelectorAll('#search-input-component .ivu-select-dropdown.ivu-auto-complete .ivu-select-dropdown-list .ivu-select-item') let hasFocus = false // 初始賦值沒有選中項(xiàng) for(let i=0; i < items.length; i++) { // 判斷有選中項(xiàng) if (items[i].className.indexOf('ivu-select-item-focus') > -1) { hasFocus = true break } } // 判斷沒有選中項(xiàng),則選中第一項(xiàng) if (!hasFocus) { this.value = this.changeList[0].cardId } selectDom.style.display = 'none' // 隱藏下拉框 } } } } </script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?webpack打包原理解析(全網(wǎng)最新最全)
webpack是讓我們可以進(jìn)行模塊化開發(fā),并且會幫助我們處理模塊間的依賴關(guān)系,這篇文章主要介紹了vue?webpack打包原理,本篇介紹的有點(diǎn)長,希望大家耐心閱讀2023-02-02使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例
這篇文章主要介紹了使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01在vue3項(xiàng)目中給頁面添加水印的實(shí)現(xiàn)方法
這篇文章主要給大家介紹一下在vue3項(xiàng)目中添加水印的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧2023-08-08vue中axios處理http發(fā)送請求的示例(Post和get)
本篇文章主要介紹了vue中axios處理http請求的示例(Post和get),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10如何構(gòu)建 vue-ssr 項(xiàng)目的方法步驟
這篇文章主要介紹了如何構(gòu)建 vue-ssr 項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08elementui[el-table]toggleRowSelection默認(rèn)多選事件無法選中問題
這篇文章主要介紹了elementui[el-table]toggleRowSelection默認(rèn)多選事件無法選中問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11