uniapp中的单选实例( radio)

 radio-groupsd

单项选择器,内部由多个 <radio> 组成。通过把多个radio包裹在一个radio-group下,实现这些radio的单选。

属性说明

属性名类型默认值说明
@changeEventHandle<radio-group> 中的选中项发生变化时触发 change 事件,event.detail = {value: 选中项radio的value}

radio

单选项目。

属性说明

属性名类型默认值说明
valueString<radio> 标识。当该 <radio> 选中时,<radio-group> 的 change 事件会携带 <radio> 的 value
checkedBooleanfalse当前是否选中
disabledBooleanfalse是否禁用
colorColorradio的颜色,同css的color

radio单选 (效果图,Html,Css,Script)

<template>
	<view class="content">
		<view class="title">
			<text>Radio用法写法</text>
		</view>
		<view class="radioInfo">
			<view class="dataInfo">
				<!-- 列表内容开始 -->
				<radio-group @change="radioChange">
					<view class="dataList" v-for="(item,index) in dataList" :key='index'
						@click="listClick(item.titleId)" :class="index === radioCurrent?'radiOn':''">
						<view class="textImg">
							<view class="img">
								<image :src="item.imgUrl" mode="widthFix"></image>
							</view>
							<view class="text">
								<text>{{item.title}}</text>
								<text>{{item.url}}</text>
							</view>
						</view>
						<view class="radioBox">
							<radio color="#2DCF8C" :value="item.titleId + ''" :checked="index === radioCurrent" />
						</view>
					</view>
				</radio-group>
				<!-- 列表内容结束 -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: [{
					imgUrl: 'https://tilin.gitee.io/super_hero_preview/static/icons/uniapp.png',
					title: 'UniApp',
					url: 'https://uniapp.dcloud.io/',
					titleId: 1
				}, {
					imgUrl: 'https://tilin.gitee.io/super_hero_preview/static/icons/github.png',
					title: 'GitHub',
					url: 'https://github.com/',
					titleId: 2
				}, {
					imgUrl: 'https://gitee.com/assets/favicon.ico',
					title: 'Giee',
					url: 'https://gitee.com/',
					titleId: 3
				}, {
					imgUrl: 'https://www.runoob.com/wp-content/uploads/2017/01/vue.png',
					title: 'Vue',
					url: 'https://cn.vuejs.org/',
					titleId: 4
				}, {
					imgUrl: 'https://www.runoob.com/wp-content/uploads/2016/02/react.png',
					title: 'React',
					url: 'https://react.docschina.org/',
					titleId: 5
				}, {
					imgUrl: 'https://www.runoob.com/wp-content/uploads/2014/06/angular.jpg',
					title: 'AngularJS',
					url: 'https://www.angularjs.net.cn/',
					titleId: 6
				}],
				radioCurrent: null,
				dataFrom: {
					titleId: null //传的id
				}
			}
		},
		methods: {
			radioChange(evt) { //单个选择框点击
				this.dataFrom.titleId = parseInt(evt.detail.value) //如果需要通过点击来知道选择的是哪个商品的id
				console.log(this.dataFrom.titleId)
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].titleId === evt.target.titleId) {
						this.radioCurrent = i;
						break;
					}
				}
			},
			listClick(titleId) { //整个数据点击
				this.dataFrom.titleId = titleId //如果需要通过点击来知道选择的是哪个商品的id
				for (let i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].titleId == titleId) {
						this.radioCurrent = i;
						break;
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	.content {
		.title {
			text-align: center;
			padding: 30rpx 0;

			text {
				color: #dedede;
				font-size: 55rpx;
				text-align: center;
				letter-spacing: 6rpx;
				text-shadow: 2rpx 2rpx 8rpx #000;
				opacity: .32;
			}
		}

		.radioInfo {
			.dataInfo {
				width: 90%;
				margin: 0 auto;

				.checkAll {
					display: flex;
					justify-content: space-between;
				}

				.radiOn {
					border: 1px solid #2DCF8C !important;
				}

				.dataList {
					display: flex;
					align-items: center;
					justify-content: space-between;
					border: 1px solid #E7E9EE;
					padding: 20rpx;
					margin-bottom: 20rpx;

					.textImg {
						display: flex;
						align-items: center;

						.img {
							border: 1px solid #E7E9EE;
							padding: 10rpx;

							image {
								width: 90rpx;
								height: 90rpx;
								display: block;
							}
						}

						.text {
							padding-left: 20rpx;

							text {
								display: block;
								font-size: 30rpx;
								color: #000;
								font-weight: bold;
							}
						}
					}
				}
			}
		}
	}
</style>

扩展

(1).radio取消选择

<template>
	<view>
		<view class="checkbox">
			<radio-group>
				<label v-for="(item,index) in scoreList" :key="index">
					<radio color="#007aff" :value="item.id + ''" :checked="item.id==numberCurrent"
						@click="number_change(index)" />
					<text>{{item.text}}</text>
				</label>
			</radio-group>
		</view>
		<view class="checkbox">
			<radio-group>
				<label v-for="(item,index) in scoreList" :key="index">
					<radio color="#007aff" :value="item.id + ''" :checked="item.id==tasteCurrent"
						@click="taste_change(index)" />
					<text>{{item.text}}</text>
				</label>
			</radio-group>
		</view>
		<button type="default" @click="add">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scoreList: [{ //评分标准
						text: '优',
						id: 1
					},
					{
						text: '中',
						id: 2
					}, {
						text: '差',
						id: 3
					}
				],
				numberCurrent: null,
				tasteCurrent: null
			}
		},
		methods: {
			add() {
				this.numberCurrent = null
				this.tasteCurrent = null
			},
			number_change(index) {
				if (this.numberCurrent == this.scoreList[index].id) {
					this.numberCurrent = null
				} else {
					this.numberCurrent = this.scoreList[index].id
				}
			},
			taste_change(index) {
				if (this.tasteCurrent == this.scoreList[index].id) {
					this.tasteCurrent = null
				} else {
					this.tasteCurrent = this.scoreList[index].id
				}
			},

		}
	}
</script>

(2).radio取消选择循环

<template>
	<view class="">
		<view class="list" v-for="(nameItem,nameIndex) in data" :key="nameIndex">
			<text>{{nameItem.name}}</text>
			<view class="checkbox">
				<radio-group>
					<label v-for="(item,index) in whether" :key="index" @click="whether_Change(nameItem,index)">
						<radio color="#007aff" :value="item.whetherId + ''"
							:checked="item.whetherId === nameItem.whetherCurrent" />
						<text>{{item.text}}</text>
					</label>
				</radio-group>
			</view>
			<view class="">
				<input type="text" placeholder="请填写" v-if="nameItem.whetherCurrent == 1" v-model="nameItem.input">
			</view>
		</view>
		<button type="default" @click="submit">提交</button>
	</view>
</template>

<script>
	import dateAndTime from "./time-week.js";
	export default {
		data() {
			return {
				whether: [{
						text: '是',
						whetherId: 1
					},
					{
						text: '否',
						whetherId: 2
					}
				],
				data: [{
						name: '张一',
						whetherCurrent: null,
						input: null
					},
					{
						name: '张二',
						whetherCurrent: null,
						input: null
					}, {
						name: '张三',
						whetherCurrent: null,
						input: null
					},
					{
						name: '张四',
						whetherCurrent: null,
						input: null
					}
				]
			}
		},
		mounted() {},
		methods: {
			whether_Change(item, index) { //是否到岗
				if (item.whetherCurrent == this.whether[index].whetherId) {
					item.whetherCurrent = null
				} else {
					item.whetherCurrent = this.whether[index].whetherId
				}
			},
			submit() {
				console.log(this.data)
			}
		}
	}
</script>

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值