微信小程序?qū)崿F(xiàn)單選按鈕
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)單選按鈕的具體代碼,供大家參考,具體內(nèi)容如下
邏輯
- 單選框的邏輯比較簡單,把所有的元素遍歷出來,等到點(diǎn)擊單選按鈕的時候,當(dāng)value值與遍歷變量值一致的時候就 把checked 設(shè)置為true,其他的時候把checked設(shè)置為 false 只需要一次循環(huán)。
- 復(fù)選框的邏輯,也不復(fù)雜,當(dāng)只有一個被選中的選項(xiàng)的時候,當(dāng)點(diǎn)擊已經(jīng)選擇的選項(xiàng)的時候,首選外層循環(huán)設(shè)置為false,這個時候 e.detail.value為零,所以無法進(jìn)入內(nèi)層循環(huán),所以被取消。當(dāng)選中未選擇的選項(xiàng)的時候,則從第一個選項(xiàng)開始遍歷,如果value值與外層相同則設(shè)置為TRUE。然后跳出內(nèi)層,繼續(xù)遍歷,當(dāng)有兩個選中的選項(xiàng)的時候,點(diǎn)擊已經(jīng)選擇的按鈕,values值會減少l,所以最后一次的外層循環(huán)無法進(jìn)入內(nèi)層,所有取消該選項(xiàng)。
舉例, 兩選一 , 默認(rèn)選中第一個;效果圖如下:
.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文件 :
Page({ ? data:{ ? ? buttons: [{ id: 1, name: '失物招領(lǐng)' }, { id: 2, name: '尋物啟事' }] ? }, ? onLoad: function() {//默認(rèn)選了第一個 ? ? 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) { ? ? ? //當(dāng)前點(diǎn)擊的位置為true即選中 ? ? ? this.data.buttons[i].checked = true; ? ? ? ? } ? ? else { ? ? ? //其他的位置為false ? ? this.data.buttons[i].checked = false; ? ? } ? } ? this.setData({ ? buttons: this.data.buttons ? }) ? } })
.wxss文件 :
.button_container{ ? display: flex; ? flex-direction: row; ? justify-content: space-around ? } ?? ? /* 按鈕未選中 */ ?.normal_button{ ? background: white; ? } ?? ? /* 按鈕選中 */ ? .checked_button{ ? background: #36ab60; ? color: white ? }```
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript代碼調(diào)試方法實(shí)例小結(jié)
這篇文章主要介紹了JavaScript代碼調(diào)試方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript錯誤信息的處理與代碼調(diào)試相關(guān)操作技巧,需要的朋友可以參考下2019-01-01js實(shí)現(xiàn)1,2,3,5數(shù)字按照概率生成
這篇文章主要介紹了js實(shí)現(xiàn)1,2,3,5數(shù)字按照概率生成,需要的朋友可以參考下2017-09-09javascript 文本框水印/占位符(watermark/placeholder)實(shí)現(xiàn)方法
html5為表單元素(type為text/password/search/url/telephone/email)新增了一個placeholder屬性,為輸入框提供一種提示2012-01-01JavaScript constructor和instanceof,JSOO中的一對歡喜冤家
現(xiàn)在流行面向?qū)ο?JavaScript當(dāng)然要迎頭趕上. 有說法JavaScript就是徹頭徹尾的OO語言,但我覺得JavaScript實(shí)現(xiàn)面向?qū)ο蟮某绦蜻€是有諸多不便的.2009-05-05javascript利用控件對windows的操作實(shí)現(xiàn)原理與應(yīng)用
假如要發(fā)送漢字的聊天框的內(nèi)容的話,我們也要從windows消息機(jī)制下手,先找到聊天消息的句柄(可以利用findwindow函數(shù)或者用spy工具哈),然后在找到上面的聊天框的句柄,接著我們就可以想這個句柄發(fā)送WM_SETTEXT的消息了2012-12-12微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕移動view標(biāo)簽的位置功能示例【附demo源碼下載】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕移動view標(biāo)簽的位置功能,涉及微信小程序事件綁定與this.setData動態(tài)修改data數(shù)值進(jìn)而改變view標(biāo)簽樣式的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12