radio单选框列表再次点击取消选中
<template>
<div class="container-box">
<div class="enterpriseChoice">
<div class="search-div">
<div class="return">
<i></i>
<p @click="$router.back()">返回</p>
</div>
<div class="search">
<i></i>
<input type="text" placeholder="请输入关键字搜索" v-model="iptValue" />
</div>
<p class="cancel" @click="iptValue = ''">取消</p>
</div>
<div class="enterprise">
<form class="mui-input-group" name="register" v-for="(item, index) in List" :key="index">
<div class="mui-input-row mui-radio mui-right enterprise-list" style="display: flex">
<label :for="'enterprise' + index" style="flex: 1" class="label">{{ item.label }}</label>
<input :class="'el' + index" :id="'enterprise' + index" name="enterprise" type="radio" :value="index" :checked="item === enterprise ? true:false" @click="toggleChecked($event)" />
</div>
</form>
</div>
</div>
<div class="index-footer">
<button @click="selectEnterprise">确认</button>
</div>
</div>
</template>
<script>
import { getDept } from "./api.js"
export default {
name: "enterpriseChoice",
components: {},
data () {
return {
deptList: [],
enterprise: "",
iptValue: "",
index: ''
}
},
props: {},
computed: {
List () {
if (this.iptValue !== "") {
return this.deptList.filter((item) =>
item.label.includes(this.iptValue)
)
} else {
return this.deptList
}
return this.List
},
},
watch: {
},
created () {
this.getList()
},
mounted () { },
methods: {
getList () {
getDept({ pid: 9 }).then((res) => {
this.deptList = res.data.content.filter((item) => {
if (item.id != 1369) {
return item
}
})
})
},
selectEnterprise () {
console.log(this.enterprise)
this.$router.push({
path: this.$route.query.path,
query: {
id: this.enterprise.id,
label: this.enterprise.label,
data: this.$route.query.data,
pathname: this.$route.name,
},
})
},
// radio点击事件
toggleChecked (e) {
// 赋值
this.enterprise = this.deptList[e.target.value]
// 判断重复点击后 取消选中 清空值 重置index
if (this.index === e.target.value) {
e.target.checked = !e.target.checked
this.enterprise = {}
this.index = ''
} else { //否则保存当前点击的索引
this.index = e.target.value
}
}
},
};
</script>
<style scoped>
.label {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
使用v-model双向数据绑定后导致无法实现再次点击取消选中的逻辑 会导致第一次点击也会取消选中 主要原因是v-model的处理总是会在自己的逻辑之前 所以取消使用v-moel
label for属性和input id属性实现点击标签选中
input name一直实现互斥
再次点击取消选中 所以是click事件而非change
作者:whh666
出处:https://www.cnblogs.com/whh666/p/16210813.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构