freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-html-and-html5/use-the-value-attribute-with-radio-buttons-and-checkboxes.md
Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

149 lines
4.6 KiB
Markdown

---
id: 5c6c06847491271903d37cfd
title: 使用单选框和复选框的 value 属性
challengeType: 0
forumTopicId: 301099
dashedName: use-the-value-attribute-with-radio-buttons-and-checkboxes
---
# --description--
提交表单时,所选项的值会发送给服务端。`radio``checkbox``value` 属性值决定了发送到服务端的实际内容。
例如:
```html
<label for="indoor">
<input id="indoor" value="indoor" type="radio" name="indoor-outdoor">Indoor
</label>
<label for="outdoor">
<input id="outdoor" value="outdoor" type="radio" name="indoor-outdoor">Outdoor
</label>
```
这里有两个 `radio` 单选框。当用户提交表单时,如果 `indoor` 选项被选中,表单数据会包含:`indoor-outdoor=indoor`。也就是所选项的 `name``value` 属性值。
如果没有指明 `value` 属性值,则会使用默认值做为表单数据提交,也就是 `on`。在这种情况下,如果用户选中 "indoor" 选项然后提交表单,表单数据则会包含 `indoor-outdoor=on`。这样的表单数据看起来不够直观,因此最好将 `value` 属性值设置为一些有意义的内容。
# --instructions--
给每一个 `radio``checkbox` 输入框添加 `value` 属性,属性值为 `input` 对应 `label` 中文本的小写形式。如 Outdoor 的 `value` 应设置为 `"outdoor"`,即 `value="outdoor"`
# --hints--
应有一个单选按钮的 `value` 属性值为 `indoor`
```js
assert(
$('label:contains("Indoor") > input[type="radio"]').filter("[value='indoor']")
.length > 0
);
```
应有一个单选按钮的 `value` 属性值为 `outdoor`
```js
assert(
$('label:contains("Outdoor") > input[type="radio"]').filter(
"[value='outdoor']"
).length > 0
);
```
应有一个复选框的 `value` 属性值为 `loving`
```js
assert(
$('label:contains("Loving") > input[type="checkbox"]').filter(
"[value='loving']"
).length > 0
);
```
应有一个复选框的 `value` 属性值为 `lazy`
```js
assert(
$('label:contains("Lazy") > input[type="checkbox"]').filter("[value='lazy']")
.length > 0
);
```
应有一个复选框的 `value` 属性值为 `energetic`
```js
assert(
$('label:contains("Energetic") > input[type="checkbox"]').filter(
"[value='energetic']"
).length > 0
);
```
# --seed--
## --seed-contents--
```html
<h2>CatPhotoApp</h2>
<main>
<p>Click here to view more <a href="#">cat photos</a>.</p>
<a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a>
<p>Things cats love:</p>
<ul>
<li>cat nip</li>
<li>laser pointers</li>
<li>lasagna</li>
</ul>
<p>Top 3 things cats hate:</p>
<ol>
<li>flea treatment</li>
<li>thunder</li>
<li>other cats</li>
</ol>
<form action="https://freecatphotoapp.com/submit-cat-photo">
<label for="indoor"><input id="indoor" type="radio" name="indoor-outdoor"> Indoor</label>
<label for="outdoor"><input id="outdoor" type="radio" name="indoor-outdoor"> Outdoor</label><br>
<label for="loving"><input id="loving" type="checkbox" name="personality"> Loving</label>
<label for="lazy"><input id="lazy" type="checkbox" name="personality"> Lazy</label>
<label for="energetic"><input id="energetic" type="checkbox" name="personality"> Energetic</label><br>
<input type="text" placeholder="cat photo URL" required>
<button type="submit">Submit</button>
</form>
</main>
```
# --solutions--
```html
<h2>CatPhotoApp</h2>
<main>
<p>Click here to view more <a href="#">cat photos</a>.</p>
<a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a>
<p>Things cats love:</p>
<ul>
<li>cat nip</li>
<li>laser pointers</li>
<li>lasagna</li>
</ul>
<p>Top 3 things cats hate:</p>
<ol>
<li>flea treatment</li>
<li>thunder</li>
<li>other cats</li>
</ol>
<form action="https://freecatphotoapp.com/submit-cat-photo">
<label for="indoor"><input id="indoor" type="radio" name="indoor-outdoor" value="indoor"> Indoor</label>
<label for="outdoor"><input id="outdoor" type="radio" name="indoor-outdoor" value="outdoor"> Outdoor</label><br>
<label for="loving"><input id="loving" type="checkbox" name="personality" value="loving"> Loving</label>
<label for="lazy"><input id="lazy" type="checkbox" name="personality" value="lazy"> Lazy</label>
<label for="energetic"><input id="energetic" type="checkbox" name="personality" value="energetic"> Energetic</label><br>
<input type="text" placeholder="cat photo URL" required>
<button type="submit">Submit</button>
</form>
</main>
```