微信小程序自定義單選框樣式實(shí)現(xiàn)單選功能
本文實(shí)例為大家分享了微信小程序自定義單選框樣式實(shí)現(xiàn)單選功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果:
選擇小車時(shí),其他類型的車取消選中。

具體思路:
用數(shù)組存幾種類型車的數(shù)據(jù),給每條數(shù)據(jù)設(shè)置點(diǎn)擊未選中的效果(checked 設(shè)為 false)。點(diǎn)擊某一種車時(shí),獲取其下標(biāo),將它的設(shè)置為選中(checked 設(shè)置 true)。
代碼如下:
// index.js
Page({
? /**
? ?* 頁(yè)面的初始數(shù)據(jù)
? ?*/
? data: {
? ? list: [{
? ? ? ? img: "../../image/car.png",
? ? ? ? name: "小車",
? ? ? ? type: "C1/C2/C3",
? ? ? ? checked: true,
? ? ? },
? ? ? {
? ? ? ? img: "../../image/trucks.png",
? ? ? ? name: "貨車",
? ? ? ? type: "A2/B2",
? ? ? ? checked: false,
? ? ? },
? ? ? {
? ? ? ? img: "../../image/passenger-car.png",
? ? ? ? name: "客車",
? ? ? ? type: "A1/A3/B1",
? ? ? ? checked: false,
? ? ? },
? ? ? {
? ? ? ? img: "../../image/motorbike.png",
? ? ? ? name: "摩托車",
? ? ? ? type: "D/E/F",
? ? ? ? checked: false,
? ? ? }
? ? ],
? },
? // 選擇
? choose: function (e) {
? ? let index = e.currentTarget.dataset.id;
? ? let list = this.data.list;
? ? for (let i = 0; i < list.length; i++) {
? ? ? this.data.list[i].checked = false;
? ? }
? ? if (list[index].checked) {
? ? ? this.data.list[index].checked = false;
? ? } else {
? ? ? this.data.list[index].checked = true;
? ? }
? ? this.setData({
? ? ? list: this.data.list
? ? })
? },
})<!-- wxml -->
? <view class="chooseType" >
? ? <view class=" {{item.checked?'chooseType-content-select':'chooseType-content'}}'" wx:for="{{list}}" wx:key="index" data-id="{{index}}" bindtap="choose">
? ? ? <image class="chooseType-content-img" src="{{item.img}}"></image>
? ? ? <view>{{item.name}}</view>
? ? ? <view class="chooseType-content-type">{{item.type}}</view>
? ? </view>
?</view>?/* wxss */
.chooseType {
? width: 100%;
? height: 200rpx;
? display: flex;
? align-items: center;
? justify-content: space-between;
? border-bottom: rgb(197, 196, 187) 1rpx solid;
? font-size: 30rpx;
? background: #FFFFFF;
}
.chooseType-content-img{
? width: 90rpx;
? height: 90rpx;
}
.chooseType-content{
? width: 25%;
? height: 100%;
? display: flex;
? align-items: center;
? flex-direction: column;
}
.chooseType-content-select{
? width: 25%;
? height: 100%;
? display: flex;
? align-items: center;
? flex-direction: column;
? background: rgb(163, 162, 162,0.2);
}
.chooseType-content-type{
? margin-top: 10rpx;
? font-size: 30rpx;
? color: #b3b0b0;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用JS實(shí)現(xiàn)一個(gè)頁(yè)面多個(gè)css樣式實(shí)現(xiàn)
在Hello,Yang中看見(jiàn)的一篇文章,感覺(jué)很有用,轉(zhuǎn)來(lái)這里……2008-05-05
關(guān)于layui的動(dòng)態(tài)圖標(biāo)不顯示的解決方法
今天小編就為大家分享一篇關(guān)于layui的動(dòng)態(tài)圖標(biāo)不顯示的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
微信小程序?qū)崿F(xiàn)打開(kāi)并下載服務(wù)器上面的pdf文件到手機(jī)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)打開(kāi)并下載服務(wù)器上面的pdf文件到手機(jī),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
innerText innerHTML的用法以及注意事項(xiàng) [推薦]
我們常常需要使用另外一些對(duì)象的屬性來(lái)實(shí)現(xiàn)動(dòng)態(tài)改變其中的文本,它們就是:innerText,outerText,innerHTML,outerHTML,千萬(wàn)要注意它們的大小寫(xiě),因?yàn)殄e(cuò)一點(diǎn)您就得不到預(yù)期的效果了。2009-05-05
JavaScript中的alert()函數(shù)使用技巧詳解
這篇文章主要介紹了JavaScript中的alert()函數(shù)使用技巧詳解,本文講解了普通彈出、帶換行的文本、使用制表符、使用變量、使用樣式等選擇,需要的朋友可以參考下2014-12-12

