这篇文章给大家分享的是有关微信小程序如何实现点击控件后选中其它反选的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
微信小程序 点击控件后选中其它反选实例详解
前言:
如果需要实现进来进行给按钮加上买一送一的样式,或者单击就选中单个按钮,只能靠css结合js进行控制了,小程序暂时没有这样的控件。
实现效果图:
微信小程序进来的时候自动进行按钮样式的初始化,这个需要一个字段做判断,加上正则表达式wxml文件:
<block wx:for="{{liuliangItems}}"> <block wx:if="{{item.one2one == 1}}"> <button class="{{item.changeColor?'selected2':'selected1'}}" type="default" id="{{item.price}}" data-id="{{item.name}}" data-one="{{item.one2one}}" data-orderid="{{item.id}}" data-number="1" bindtap="setPrice">{{item.name}}</button> </block> <block wx:else> <button class="{{item.changeColor?'selected':'normal'}}" type="default" id="{{item.price}}" data-id="{{item.name}}" data-one="{{item.one2one}}" data-orderid="{{item.id}}" data-number="1" bindtap="setPrice">{{item.name}}</button> </block> </block>
wxss文件
.normal{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; color:#1aad19; border:1px solid #1aad19; background-color:transparent; } .selected{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: #F75000; color: white; } .selected1{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: transparent; border:1px solid #1aad19; color:#1aad19; background-image: url(https://wxcx.llzt.net/images/hot.png) ; background-position:31px 0px; background-repeat:no-repeat; background-size:62%; } .selected2{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: #F75000; color: white; background-image: url(https://wxcx.llzt.net/images/hot.png); background-position:31px 0px; background-repeat:no-repeat; background-size:62%; }
现在的方法把集合进行循环,然后获取当前点击的这个按钮进行比较,然后进行样式的修改
for (var i = 0; i < this.data.liuliangItems.length; i++) { if (e.target.dataset.orderid == this.data.liuliangItems[i].id) { txtArray1[i] = { id: this.data.liuliangItems[i].id, changeColor: true, price: this.data.liuliangItems[i].price, name: this.data.liuliangItems[i].name, one2one: this.data.liuliangItems[i].one2one } } else { txtArray1[i] = { id: this.data.liuliangItems[i].id, changeColor: false, price: this.data.liuliangItems[i].price, name: this.data.liuliangItems[i].name, one2one: this.data.liuliangItems[i].one2one } } }
免责声明:本站发布的内容(图片、视频和文字)以原创、来自互联网转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:ts@56dr.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
Copyright © 2009-2021 56dr.com. All Rights Reserved. 特网科技 版权所有 珠海市特网科技有限公司 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 中国互联网举报中心 增值电信业务经营许可证B2 建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站