1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<script setup lang="ts">
import { ref } from 'vue';
// 当前选中的值
const selected = ref('');
// 选项列表
const options = ref([
{
id: 1,
text: 'A.猫',
value: 'A',
},
{
id: 2,
text: 'B.狗',
value: 'B',
},
{
id: 3,
text: 'C.老鼠',
value: 'C',
},
]);
</script>
<template>
<div>
<div>selected: {{ selected }}</div>
<div v-for="option in options" :key="option.id">
<!-- 绑定 input 的 id 和 value -->
<input
type="radio"
:id="`option-${option.id}`"
:value="option.value"
v-model="selected"
/>
<!-- 绑定 label 的 for -->
<label :for="`option-${option.id}`">{{ option.text }}</label>
</div>
</div>
</template>
|