radio.md 3.8 KB
Newer Older
M
mehaotian 已提交
1 2
#### radio-group

W
wanganxp 已提交
3
单项选择器,内部由多个 ``<radio>`` 组成。通过把多个`radio`包裹在一个`radio-group`下,实现这些`radio`的单选。
M
mehaotian 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

**属性说明**

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

#### radio

单选项目。

**属性说明**

|属性名|类型|默认值|说明|
|:-|:-|:-|:-|
|value|String||``<radio>`` 标识。当该 ``<radio>`` 选中时,``<radio-group>`` 的 change 事件会携带 ``<radio>`` 的 value|
|checked|Boolean|false|当前是否选中|
|disabled|Boolean|false|是否禁用|
|color|Color||radio的颜色,同css的color|

雪洛's avatar
雪洛 已提交
24
**示例** [查看演示](https://hellouniapp.dcloud.net.cn/pages/component/radio/radio)
M
mehaotian 已提交
25
 
W
wanganxp 已提交
26
以下示例代码,来自于[hello uni-app项目](https://github.com/dcloudio/hello-uniapp),推荐使用HBuilderX,新建uni-app项目,选择hello uni-app模板,可直接体验完整示例。
M
mehaotian 已提交
27
```html
W
wanganxp 已提交
28
<!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
M
mehaotian 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
<template>
	<view>
		<view class="uni-padding-wrap">
			<view class="uni-title">默认样式</view>
			<view>
				<label class="radio"><radio value="r1" checked="true" />选中</label>
				<label class="radio"><radio value="r2" />未选中</label>
			</view>
		</view>
		<view class="uni-title uni-common-mt uni-common-pl">推荐展示样式</view>
		<view class="uni-list">
			<radio-group @change="radioChange">
				<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
					<view>
						<radio :value="item.value" :checked="index === current" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</radio-group>
		</view>
	</view>
</template>
```
```javascript
export default {
    data() {
        return {
            items: [{
                    value: 'USA',
                    name: '美国'
                },
                {
                    value: 'CHN',
                    name: '中国',
                    checked: 'true'
                },
                {
                    value: 'BRA',
                    name: '巴西'
                },
                {
                    value: 'JPN',
                    name: '日本'
                },
                {
                    value: 'ENG',
                    name: '英国'
                },
                {
                    value: 'FRA',
                    name: '法国'
                },
            ],
            current: 0
        }
    },
    methods: {
        radioChange: function(evt) {
            for (let i = 0; i < this.items.length; i++) {
                if (this.items[i].value === evt.target.value) {
                    this.current = i;
                    break;
                }
            }
        }
    }
}
```
 
雪洛's avatar
雪洛 已提交
98
![uniapp](https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/44bec6b0-4f30-11eb-a16f-5b3e54966275.png)
M
mehaotian 已提交
99 100 101


**注意**
102
- radio的默认颜色,在不同平台不一样。微信小程序是绿色的,字节跳动小程序为红色,其他平台是蓝色的。更改颜色使用color属性。
M
mehaotian 已提交
103
- 如需调节radio大小,可通过css的scale方法调节,如缩小到70%`style="transform:scale(0.7)"`
W
wanganxp 已提交
104
- radio不是checkbox,点击一个已经选中的radio,不会将其取消选中
W
wanganxp 已提交
105 106 107

**扩展**
- uni-ui提供了增强的uni-data-checkbox组件,基于[datacom规范](/component/datacom),只需传入data数据,即可自动生成一组单选框,使用方式更简洁,并且支持[uni-forms](https://ext.dcloud.net.cn/plugin?id=2773)的表单验证。uni-data-checkbox组件详见[https://ext.dcloud.net.cn/plugin?id=3456](https://ext.dcloud.net.cn/plugin?id=3456)