博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信小程序 单选按钮 最佳
阅读量:4609 次
发布时间:2019-06-09

本文共 1042 字,大约阅读时间需要 3 分钟。

wxml:

 

<view class='button_container'>
<block wx:for="{
{buttons}}" wx:key="buttons">
<button class='{
{item.checked?"checked_button":"normal_button"}}' data-id='{
{item.id}}' bindtap='radioButtonTap'>{
{item.name}}</button>
</block>
</view>
 
js;
data:{
buttons: [{ id: 1, name: '失物招领' }, { id: 2, name: '寻物启事' }]
}
onLoad: function() {//默认选了第一个
this.data.buttons[0].checked = true;
this.setData({
buttons: this.data.buttons,
})
}
 
radioButtonTap: function (e) {
console.log(e)
let id = e.currentTarget.dataset.id
console.log(id)
for (let i = 0; i < this.data.buttons.length; i++) {
if (this.data.buttons[i].id == id) {
//当前点击的位置为true即选中
this.data.buttons[i].checked = true;
}
else {
//其他的位置为false
this.data.buttons[i].checked = false;
}
}
this.setData({
buttons: this.data.buttons
})
},
 
css:
.button_container{
display: flex;
flex-direction: row;
justify-content: space-around
}
/* 按钮未选中 */
.normal_button{
background: white;
}
/* 按钮选中 */
.checked_button{
background: #36ab60;
color: white
}

转载于:https://www.cnblogs.com/lsyy2017/p/11564524.html

你可能感兴趣的文章
Eclipse插件项目中读取文件
查看>>
jquery定义链接跳转的高亮显示
查看>>
CheckListBox怎样得到多选值?
查看>>
三道题(关于虚表指针位置/合成64位ID/利用栈实现四则运算)
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
mysql 数据表操作 目录
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
jQuery中事件绑定与解绑
查看>>
js原生Ajax的封装与使用
查看>>
周总结6
查看>>
PostgreSQL 务实应用(二/5)插入冲突
查看>>
一种公众号回复关键词机制
查看>>
java多线程入门学习(一)
查看>>
基于 Web 的 Go 语言 IDE - Wide 1.1.0 公布!
查看>>
nyist oj 138 找球号(二)(hash 表+位运算)
查看>>
Movidius软件手册阅读 2017-09-04
查看>>
ytu 1910:字符统计(水题)
查看>>
201671030110 姜佳宇 实验三作业互评与改进
查看>>
mysql-5.6.15 开启二进制文件
查看>>