vue結(jié)合v-for和input實現(xiàn)多選列表checkbox功能
在 Vue 中可以使用v-for
指令結(jié)合數(shù)據(jù)綁定來實現(xiàn)多選列表。以下是具體步驟:
一、HTML 結(jié)構(gòu)
<div id="app"> <ul> <li v-for="item in items" :key="item.id"> <input type="checkbox" v-model="selectedItems" :value="item"> {{ item.name }} </li> </ul> </div>
在上述代碼中,使用v-for
循環(huán)遍歷items
數(shù)組,為每個元素生成一個<li>
標(biāo)簽。每個<li>
標(biāo)簽中包含一個復(fù)選框和對應(yīng)的文本內(nèi)容。復(fù)選框的v-model
綁定到selectedItems
數(shù)組,用于存儲被選中的項。
二、Vue 實例
new Vue({ el: '#app', data: { items: [ { id: 1, name: '選項 1' }, { id: 2, name: '選項 2' }, { id: 3, name: '選項 3' } ], selectedItems: [] } });
在 Vue 實例中,定義了items
數(shù)組作為原始數(shù)據(jù),以及selectedItems
數(shù)組用于存儲被選中的項。
這樣,當(dāng)用戶勾選復(fù)選框時,對應(yīng)的項會被添加到selectedItems
數(shù)組中,取消勾選時會從該數(shù)組中移除。
代碼實例
template
<div class="list"> <div class="car-item" v-for="item in carList" :key="item.id" > <input class="checkbox" type="checkbox" :value="item" @change="carSelectListChange" v-model="carSelectList"/> <div class="name">{{item.name}}</div> </div> </div>
js
<script> export default { name: "index", data(){ return{ // 車輛表格數(shù)據(jù) carList: [], carSelectList:[], } }, methods:{ carSelectListChange(){ console.log(this.carSelectList) } } } </script>
到此這篇關(guān)于vue結(jié)合v-for和input實現(xiàn)多選列表checkbox的文章就介紹到這了,更多相關(guān)vue多選列表checkbox內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2+SpringBoot實現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
本文主要介紹了Vue2+SpringBoot實現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10