vue radio單選框,獲取當前項(每一項)的value值操作
前言
本文使用了lable關(guān)聯(lián)選中,實際使用中如果不需要,直接將循環(huán)語句 v-for 寫在 input標簽上就可以
1、使用v-for循環(huán)的radio單選框
01)需要注意的是,這是使用的是 change 事件,而不是 click 點擊事件
<template> <div> <label v-for="(item, index) in radioData" :key="index"> <input type="radio" v-model="radioVal" :value="item.value" @change="getRadioVal" /> {{ item.value }} </label> </div> </template> <script> export default { data() { return { radioData: [ { value: '全部' }, { value: '部分' }, { value: '零散' } ], radioVal: '全部' // 用于設(shè)置默認選中項 }; }, methods: { getRadioVal() { console.log(this.radioVal); } } }; </script>
2、不使用v-for循環(huán)的radio單選框
01)需要注意的是,這是使用的是 change 事件,而不是 click 點擊事件
<template> <div> <label><input v-model="radioVal" type="radio" value="全部" @change="getRadioVal">全部</label> <label><input v-model="radioVal" type="radio" value="部分" @change="getRadioVal">部分</label> <label><input v-model="radioVal" type="radio" value="零散" @change="getRadioVal">零散</label> </div> </template> <script> export default { data() { return { radioVal: '全部' // 用于設(shè)置默認選中項 }; }, methods: { getRadioVal() { console.log(this.radioVal); } } }; </script>
點擊每一項獲得當前項的value值,使用v-for 和不使用v-for 實現(xiàn)的效果是一樣的
這里就不分開寫效果圖了
如果本篇文章對你有幫助的話,很高興能夠幫助上你。
補充知識:vue綁定單選框(radio)和復(fù)選框(CheckBox)
html部分
<div style="width:500px;margin:50px auto;display:flex;flex-direction:column;"> <div style="font-weight:600;font-size:18px">問卷調(diào)查</div> <div v-for="(item,index) in question" :key="index" style="padding-top:10px"> <div style="margin-bottom:10px">{{item.title}}</div> <div v-if="item.sex" style="display:flex;align-items:center;"> <div v-for="(item2,index2) in item.sex" :key="index2" @click="chooseSex(item2)" style="margin-right:20px"> <input type="radio" :value="item2" v-model="radio2"> <span> {{item2}}</span> </div> </div> <div v-if="item.item" style="display:flex;align-items:center;"> <div v-for="(item3,index3) in item.item" :key="index3" @click="chooseHobbied(item3)" style="margin-right:20px"> <input type="checkbox" :value="item3" v-model="checkbox"><span> {{item3}}</span> </div> </div> </div> </div>
vue數(shù)據(jù)綁定
data() { return { radio2:'', checkbox:[], question:[ { title:"1、請選擇你的性別", sex:[ '男','女' ] }, { title:"2、請選擇你的愛好", item:[ '打球','讀書','畫畫','游泳','跑步' ] } ], }; },
js部分
//單選框radio選中值的改變 chooseSex(item){ this.radio2 = item; console.log("點擊",item,"值",this.radio2); }, //復(fù)選框checkbox多項選擇后的值,及取消選中后的其他值 chooseHobbied(item){ if(box.indexOf(item) === -1){ box.push(item); this.checkbox = box; console.log("點擊",item,"值",box); }else{ box.splice(box.indexOf(item),1); console.log("box值",box); this.checkbox = box; } },
以上這篇vue radio單選框,獲取當前項(每一項)的value值操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼
本篇文章主要介紹了Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼,非常具有實用價值,需要的朋友可以參考下2017-11-11vue項目如何實現(xiàn)ip和localhost同時訪問
這篇文章主要介紹了vue項目如何實現(xiàn)ip和localhost同時訪問,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue2老項目中node-sass更換dart-sass的操作方法
這篇文章主要介紹了vue2老項目中node-sass更換dart-sass的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-07-07ant desing vue table 實現(xiàn)可伸縮列的完整例子
最近在使用ant-design-vue做表格時,遇到要做一個可伸縮列表格的需求,在網(wǎng)上一直沒有找到好的方法,于是小編動手自己寫個可以此功能,下面小編把ant desing vue table 可伸縮列的實現(xiàn)代碼分享到腳本之家平臺供大家參考2021-05-05vue響應(yīng)式系統(tǒng)之observe、watcher、dep的源碼解析
這篇文章主要介紹了vue響應(yīng)式系統(tǒng)之observe、watcher、dep的源碼解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04