微信小程序?qū)崿F(xiàn)復(fù)選框效果
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)復(fù)選框片展示的具體代碼,供大家參考,具體內(nèi)容如下
樣式部分你們自由發(fā)揮,反正這里是什么都沒(méi)寫(xiě)的,選中和沒(méi)選中直接用這個(gè)this.data.arrStatus[checkIndex]去判斷就行了,之后你們都懂的。
效果預(yù)覽:

js部分
// page/index/index.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
items: [
{ name: 'USA', value: '美國(guó)' },
{ name: 'CHN', value: '中國(guó)' },
{ name: 'BRA', value: '巴西' },
{ name: 'JPN', value: '日本' },
{ name: 'ENG', value: '英國(guó)' },
{ name: 'TUR', value: '法國(guó)' },
],
arr: [],
arrStatus: []
},
check: function (e) {
//獲取當(dāng)前選中的值
var checkValue = e.currentTarget.dataset.val;
//獲取當(dāng)前選中的下標(biāo)
var checkIndex = e.currentTarget.dataset.index;
//當(dāng)前選中的取反值
this.data.arrStatus[checkIndex] = !this.data.arrStatus[checkIndex];
if (this.data.arrStatus[checkIndex]) {
//如果當(dāng)前為選中狀態(tài)則將值插入進(jìn)數(shù)組中
this.data.arr.push(checkValue);
}else{
//如果當(dāng)前為未選中狀態(tài)則將值從數(shù)組中刪除并返回一個(gè)新的數(shù)組
for (var i in this.data.arr) {
if (this.data.arr[i] == checkValue) {
this.data.arr.splice(i);
}
}
}
//打印當(dāng)前所選中的數(shù)據(jù)
console.log(this.data.arr);
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
//設(shè)置數(shù)組中每一個(gè)數(shù)據(jù)的狀態(tài)
for (var i in this.data.items) {
this.data.arrStatus[i] = false;
}
},
})wxml部分:
<block wx:for='{{ items }}'>
? <text data-index='{{ index }}' data-val='{{ item.value }}' catchtap='check'>{{ item.value }}</text>
</block>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
每天一篇javascript學(xué)習(xí)小結(jié)(Array數(shù)組)
這篇文章主要介紹了javascript中的Array數(shù)組知識(shí)點(diǎn),對(duì)數(shù)組的基本使用方法,以及各種方法進(jìn)行整理,感興趣的小伙伴們可以參考一下2015-11-11
微信小程序中上傳圖片并進(jìn)行壓縮的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序中上傳圖片并進(jìn)行壓縮的實(shí)現(xiàn)代碼,文中提到了上傳圖片并進(jìn)行壓縮的屬性值,需要的朋友可以參考下2018-08-08
Mozilla 表達(dá)式 __noSuchMethod__
這是一個(gè)很特殊的方法,但是其存在的意義很大。不過(guò)很可惜只有firefox支持了。一個(gè)簡(jiǎn)單的例子解釋一下它的用處2009-04-04

