vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問題
vue radio單選框如何取消選中狀態(tài)
客戶需求
單選radio選中后,再次點(diǎn)擊需要可以取消選擇功能
頁面有很組多單選,要實(shí)現(xiàn)一個(gè)方法就能兼容
話不多說直接上代碼
? ?<span v-for="item in radioData" :key="item.value">
? ? ? ? ? ? ? ? ? ? ? ? ? <input type="radio" @click="chcekRadio($event)" name="abutmentVal" v-model="myData.abutmentVal" :id="'Abutment'+ item.id" :value="item.id" />
? ? ? ? ? ? ? ? ? ? ? ? ? <label :for="'Abutment'+ item.id" :value="item.id">{{ item.name }}</label>
? ? ? ? ? ? ? ? ? ? ? ? </span>?//vue中radio單選框單擊取消選中狀態(tài)
? ? chcekRadio ($event) {
? ? ? let _this = this;
? ? ? let objVal = _this.myData[$event.target.name];
? ? ? window.setTimeout(() => {
? ? ? ? if (!!objVal && objVal == $event.target.value) {
? ? ? ? ? $event.target.checked = false
? ? ? ? ? _this.myData[$event.target.name] = ''
? ? ? ? }
? ? ? }, 0);
? ? },如何獲取radio的選中值 、選中狀態(tài)
方法1
設(shè)置v-model
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id='app'>
<input type="radio" name="test" v-for="(item,index) in list" :value="item.value" v-model="checkedValue">
<button @click="test">獲取選中的值</button>
</div>
<script>
var vm = new Vue({
el:'#app',
data(){
return{
checkedValue:'',
list:[{value:1},{value:2},{value:3}]
}
},
methods:{
test(){
console.log('被選中的值為:'+this.checkedValue)
}
}
});
</script>
</body>
</html> 方法2
如果不想每次點(diǎn)擊都判斷,那就提供一個(gè)思路,定義一個(gè)radio數(shù)組list,每項(xiàng)設(shè)置一個(gè)isCheck標(biāo)識(shí),循環(huán)該數(shù)組渲染radio,點(diǎn)擊按鈕時(shí)再統(tǒng)一去判斷
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id='app'>
<input type="radio" name="test" v-for="(item,index) in list" :value="item.value" :checked="item.isCheck" @change="changeInput(index)">
<button @click="test">獲取選中的值</button>
</div>
<script>
var vm = new Vue({
el:'#app',
data(){
return{
list:[{value:1,isCheck:false},{value:2,isCheck:false},{value:3,isCheck:false}]
}
},
methods:{
changeInput(index){
this.list.map((v,i)=>{
if(i==index){
v.isCheck = true
}else{
v.isCheck = false
}
})
},
test(){
this.list.map((v,i)=>{
if(v.isCheck){
console.log('被選中的值為:'+v.value)
}
})
}
}
});
</script>
</body>
</html> 總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue watch監(jiān)聽取不到this指向的數(shù)問題
這篇文章主要介紹了vue watch監(jiān)聽取不到this指向的數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09
vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案
今天遇到一個(gè)復(fù)制粘貼的需求,研究之后發(fā)現(xiàn)太簡(jiǎn)單了,這篇文章主要給大家介紹了關(guān)于vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
v-distpicker地區(qū)選擇器組件使用實(shí)例詳解
代碼添加了一個(gè)vDistpickerHandle的事件處理函數(shù)對(duì)地區(qū)選擇器中的數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)存儲(chǔ)到form對(duì)象的相應(yīng)屬性中,方便數(shù)據(jù)提交,這篇文章主要介紹了v-distpicker地區(qū)選擇器組件使用,需要的朋友可以參考下2024-02-02
詳細(xì)講解如何創(chuàng)建, 發(fā)布自己的 Vue UI 組件庫(kù)
當(dāng)我們自己開發(fā)了一個(gè) _UI Component_, 需要在多個(gè)項(xiàng)目中使用的時(shí)候呢? 我們首先想到的可能是直接復(fù)制一份過去對(duì)嗎?我們?yōu)槭裁床话l(fā)布一個(gè) UI 組件庫(kù)給自己用呢?下面小編和大家來一起學(xué)習(xí)下吧2019-05-05
詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)
這篇文章主要介紹了詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue3中關(guān)于ref和reactive的區(qū)別分析
這篇文章主要介紹了vue3關(guān)于ref和reactive的區(qū)別分析,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-06-06

