vue2 radio单选
<
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单选框单击取消选中状态
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的选中值 、选中状态
方法1 设置v-model
方法1 设置v-model
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>test</
title
>
</
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 如果不想每次点击都判断,那就提供一个思路,定义一个radio数组list,每项设置一个isCheck标识,循环该数组渲染radio,点击按钮时再统一去判断
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>test</
title
>
</
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
>
posted on 2024-01-06 17:24 zyp_java_net 阅读(168) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2023-01-06 面了几个说自己精通 Vue 的同学,实在一言难尽……
2022-01-06 node.js16.13.0版本 对应node-sass sass-loader 可行版本号