2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08834
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 创建一组单选按钮
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 0
|
2019-12-26 20:05:59 +08:00
|
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/cNWKvuR'
|
|
|
|
forumTopicId: 16822
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
`radio buttons`(单选按钮)就好比单项选择题,正确答案只有一个。
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
单选按钮是 `input` 选择框的一种类型。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
每一个单选按钮都应该嵌套在它自己的 `label`(标签)元素中。这样,我们相当于给 `input` 元素和包裹它的 `label` 元素建立起了对应关系。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
所有关联的单选按钮应该拥有相同的 `name` 属性。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2019-12-26 20:05:59 +08:00
|
|
|
下面是一个单选按钮的例子:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<label>
|
|
|
|
<input type="radio" name="indoor-outdoor">Indoor
|
|
|
|
</label>
|
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
使得 `input` 与 `label` 关联的最佳实践是在 `label` 元素上设置 `for` 属性,让其值与单选按钮的 `id` 属性值相同。
|
2019-12-26 20:05:59 +08:00
|
|
|
|
|
|
|
```html
|
|
|
|
<label for="indoor">
|
|
|
|
<input id="indoor" type="radio" name="indoor-outdoor">Indoor
|
|
|
|
</label>
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
给表单添加两个单选按钮,一个叫 `indoor` 另一个叫 `outdoor`。并将单选按钮的 `name` 属性值设置为 `indoor-outdoor`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
页面上应存在两个单选按钮元素。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('input[type="radio"]').length > 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
应设置单选按钮的 `name` 属性值为 `indoor-outdoor`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('input[type="radio"]').filter("[name='indoor-outdoor']").length > 1);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
每个单选按钮都应嵌套进它自己的 `label` 元素中。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('label > input[type="radio"]:only-child').length > 1);
|
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
每一个 `label` 元素都有结束标签。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
code.match(/<\/label>/g) &&
|
|
|
|
code.match(/<label/g) &&
|
|
|
|
code.match(/<\/label>/g).length === code.match(/<label/g).length
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
其中一个 `label` 元素的文本为 `indoor`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('label')
|
|
|
|
.text()
|
|
|
|
.match(/indoor/gi)
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
其中一个 `label` 元素的文本为 `outdoor`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('label')
|
|
|
|
.text()
|
|
|
|
.match(/outdoor/gi)
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
所有的单选按钮都应该包含在 `form` 表单中。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('label').parent().get(0).tagName.match('FORM'));
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|