微信小程序單選框組應(yīng)用詳解
本文實例為大家分享了微信小程序單選框組應(yīng)用的具體代碼,供大家參考,具體內(nèi)容如下
需求概述
有一個核選項數(shù)組,里面存放著核選項名稱、內(nèi)容、ID、選擇狀態(tài)。選擇狀態(tài)有未選擇、符合、不符合三種,默認全部為未選擇。通過wx:for將數(shù)組渲染到頁面,每個核選項都有兩個單選按鈕,分別是符合和不符合按鈕,點擊對應(yīng)按鈕會將改變對應(yīng)那條元素中的選擇狀態(tài)的值,且點擊不符合時,會顯示出一個文本域,讓用戶輸入不符合原因。
頁面最下面有一個提交按鈕,點擊時會遍歷核選項數(shù)組,若有選擇狀態(tài)為未選擇的項,則無法提交,并提醒。
效果圖如下
直接上代碼
wxml文件
<block wx:for="{{SmallItem}}" wx:key="itemID"> ? ? <view class="SmallItemView"> ? ? ? ? <view class="infoView"> ? ? ? ? ? ? <!-- 核選項的基本信息,例如名稱、內(nèi)容 --> ? ? ? ? ? ? <view class="SmallItemName"> ? ? ? ? ? ? ? ? <label for="">核選項名稱:</label> ? ? ? ? ? ? ? ? <text>{{item.itemName}}</text> ? ? ? ? ? ? </view> ? ? ? ? ? ? <view class="SmallItemContent" > ? ? ? ? ? ? ? ? <label for="">核選項內(nèi)容:</label> ? ? ? ? ? ? ? ? {{item.itemContent}} ? ? ? ? ? ? </view> ? ? ? ? </view> ? ? ? ? <view class="reasonView" hidden="{{item.AccordWith=='不符合'?false:true}}"> ? ? ? ? ? ? <!-- 不符合的時候顯示的文本域 --> ? ? ? ? ? ? <textarea placeholder="請輸入不符合原因"? ? ? ? ? ? ? ? ? ? ? ? maxlength="-1"? ? ? ? ? ? ? ? ? ? ? ? bindblur="inputChange" ? ? ? ? ? ? ? ? ? ? ? data-index="{{index}}"> ? ? ? ? ? ? </textarea> ? ? ? ? </view> ? ? ? ? <view class="counterView"> ? ? ? ? ? ? 第{{index+1}}項/共{{SmallItem.length}}項 ? ? ? ? </view> ? ? ? ? <view class="radioView"> ? ? ? ? ? ? <!-- 核選項的單選框 --> ? ? ? ? ? ? <radio-group bindchange="radioChange" data-index="{{index}}"> ? ? ? ? ? ? ? ? <radio value="符合">符合</radio> <!--前面有兩個中文全角空格--> ? ? ? ? ? ? ? ? <radio value="不符合">不符合</radio> ? ? ? ? ? ? </radio-group> ? ? ? ? </view> ? ? </view> </block> <button type="default" class="SubmitBtn" bind:tap="HXXSubmit">提交</button>
wxss文件
.SmallItemView { ? ? background-color: rgba(169, 169, 169, 0.329); ? ? padding: 5px; ? ? margin: 3px; ? ? border-radius: 3px; ? ? margin-bottom: 10px; ? ? /* box-shadow: 0px 2px 4px 2px #120f0f5c; */ } .infoView { ? ? margin-bottom: 5px; } .SmallItemName { ? ? /* 核選項名稱view */ ? ? background-color: white; ? ? margin-bottom: 2px; ? ? border-radius: 3px; ? ? padding: 2px; } .SmallItemName>label { ? ? display: block; ? ? font-weight: bold; } .SmallItemContent { ? ? /* 核選項內(nèi)容 */ ? ? background-color: white; ? ? margin-bottom: 2px; ? ? border-radius: 3px; ? ? padding: 2px; } .SmallItemContent>label { ? ? display: block; ? ? font-weight: bold; } .reasonView { ?? ?/*不符合原因文本域樣式*/ ? ? background-color: white; ? ? padding: 2px; ? ? border-radius: 3px; ? ? margin-bottom: 3px; } .radioView { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text-align: center; } .counterView { ? ? text-align: center; ? ? font-size: x-small; ? ? background-color: white; ? ? margin-bottom: 5px; } .SubmitBtn { ? ? /* 提交按鈕樣式 */ ? ? width: 90% !important; ? ? margin: 5px 10px; }
js文件
// pages/RadioDemo/RadioDemo.js Page({ ? ? data: { ? ? ? ? SmallItem: [ ? ? ? ? ? ? { itemID: "1", itemName: "核選項1", itemType: "核選項", itemContent: "核選項內(nèi)容1", AccordWith: "未選", textareaContent: "" }, ? ? ? ? ? ? { itemID: "2", itemName: "核選項2", itemType: "核選項", itemContent: "核選項內(nèi)容2", AccordWith: "未選", textareaContent: "" }, ? ? ? ? ? ? { itemID: "3", itemName: "核選項3", itemType: "核選項", itemContent: "核選項內(nèi)容3", AccordWith: "未選", textareaContent: "" }, ? ? ? ? ? ? { itemID: "4", itemName: "核選項4", itemType: "核選項", itemContent: "核選項內(nèi)容4", AccordWith: "未選", textareaContent: "" }, ? ? ? ? ? ? { itemID: "5", itemName: "核選項5", itemType: "核選項", itemContent: "核選項內(nèi)容5", AccordWith: "未選", textareaContent: "" }, ? ? ? ? ? ? { itemID: "6", itemName: "核選項6", itemType: "核選項", itemContent: "核選項內(nèi)容6", AccordWith: "未選", textareaContent: "" } ? ? ? ? ] ? ? }, ? ? radioChange: function(e) { ? ? ? ? // console.log("radio的值為:" + e.detail.value); //獲取radio的值 ? ? ? ? // console.log("元素下標為:" + e.currentTarget.dataset.index); //獲取元素在數(shù)組中的下標 ? ? ? ? let ValRadio = e.detail.value; ? ? ? ? let EleIndex = e.currentTarget.dataset.index; ? ? ? ? let key = 'SmallItem[' + EleIndex + '].AccordWith'; ? ? ? ? //更改數(shù)組SmallItem[EleIndex]中AccordWith的值 ? ? ? ? this.setData({ ? ? ? ? ? ? [key]: ValRadio ? ? ? ? }); ? ? ? ? // console.log("SmallItem數(shù)組改變后", this.data.SmallItem); ? ? }, ? ? HXXSubmit: function(e) { ? ? ? ? //點擊提交按鈕,會獲取data中的數(shù)組SmallItem,并且遍歷其中的AccordWith屬性的值 ? ? ? ? //若有AccordWith的值為“未選”,則彈出提示框,且事件執(zhí)行結(jié)束 ? ? ? ? //否則將數(shù)組傳回后臺,進行保存 ? ? ? ? let arSmallItem = this.data.SmallItem; //獲取data中的數(shù)組SmallItem ? ? ? ? for (let i = 0; i < arSmallItem.length; i++) { ? ? ? ? ? ? //遍歷數(shù)組 ? ? ? ? ? ? if (arSmallItem[i].AccordWith == "未選") { ? ? ? ? ? ? ? ? // console.log(arSmallItem[i].itemName + "未進行選擇,請選擇后提交"); ? ? ? ? ? ? ? ? wx.showToast({ ? ? ? ? ? ? ? ? ? ? title: '第' + (i + 1) + '項未進行核選\n請核選后提交', ? ? ? ? ? ? ? ? ? ? icon: 'none', ? ? ? ? ? ? ? ? ? ? duration: 2000 ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? console.log("所有核選項已選擇"); ? ? ? ? //程序能執(zhí)行到這里,說明所有核選項都已經(jīng)進行了選擇,可以進行數(shù)據(jù)保存操作 ? ? ? ? // var reqTask = wx.request({ ? ? ? ? // ? ? url: '', ? ? ? ? // ? ? data: {}, ? ? ? ? // ? ? header: { 'content-type': 'application/json' }, ? ? ? ? // ? ? method: 'GET', ? ? ? ? // ? ? dataType: 'json', ? ? ? ? // ? ? responseType: 'text', ? ? ? ? // ? ? success: (result) => { ? ? ? ? // ? ? }, ? ? ? ? // ? ? fail: () => {}, ? ? ? ? // ? ? complete: () => {} ? ? ? ? // }); ? ? }, ? ? inputChange: function(e) { ? ? ? ? // console.log(e); ? ? ? ? let index = e.currentTarget.dataset.index; //獲取元素在數(shù)組中的下標 ? ? ? ? let content = e.detail.value; //獲取在文本域中輸入的值 ? ? ? ? let key = 'SmallItem[' + index + '].textareaContent'; ? ? ? ? this.setData({ ? ? ? ? ? ? [key]: content ? ? ? ? }); ? ? ? ? console.log("SmallItem數(shù)組改變后", this.data.SmallItem); ? ? } })
當存在核選項未進行選擇時,點擊保存,彈窗提醒效果圖
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實現(xiàn)雙擊內(nèi)容變?yōu)榭删庉嫚顟B(tài)
在一些網(wǎng)站上我們經(jīng)??吹浇换バ院軓姷墓δ?。一些用戶資料可以直接雙擊出現(xiàn)文本框,并在此輸入新的資料即可修改,無需再按確定按鈕等。怎么實現(xiàn)的呢?今天小編給大家分享JS實現(xiàn)雙擊內(nèi)容變?yōu)榭删庉嫚顟B(tài),需要的的朋友參考下2017-03-03微信小程序?qū)W習筆記之函數(shù)定義、頁面渲染圖文詳解
這篇文章主要介紹了微信小程序?qū)W習筆記之函數(shù)定義、頁面渲染,結(jié)合實例形式較為詳細的分析了微信小程序中函數(shù)的定義、生命周期、模板調(diào)用、樣式控制等操作技巧,并配合圖文形式進行了詳細說明,需要的朋友可以參考下2019-03-03詳解promise.then,process.nextTick, setTimeout 以及 setImmediate的
這篇文章主要介紹了詳解promise.then,process.nextTick, setTimeout 以及 setImmediate的執(zhí)行順序2018-11-11Javascript設(shè)計模式之發(fā)布訂閱模式
發(fā)布---訂閱模式又叫觀察者模式,它定義了對象間的一種一對多的關(guān)系,讓多個觀察者對象同時監(jiān)聽某一個主題對象,當一個對象發(fā)生改變時,所有依賴于它的對象都將得到通知2022-12-12JavaScript中const關(guān)鍵字的用法及特性
該文章講解了JavaScript中const關(guān)鍵字的用法以及它的一些特性,該關(guān)鍵字用于創(chuàng)建常量,即一旦賦值之后就不能再修改,但是,使用?const創(chuàng)建的對象和數(shù)組卻可以被修改,本文通過講解“賦值”和“變異”之間的重要區(qū)別,詳細解釋了這一現(xiàn)象,需要的朋友可以參考下2023-05-05