4.5 KiB
4.5 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5c6c06847491271903d37cfd | 使用单选框和复选框的 value 属性 | 0 | 301099 | use-the-value-attribute-with-radio-buttons-and-checkboxes |
--description--
提交表单时,所选项的值会发送给服务端。 radio
和 checkbox
的 value
属性值决定了发送到服务端的实际内容。
例如:
<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
属性。 使用输入标签的文本,小写形式,作为属性的值。
--hints--
应有一个单选按钮的 value
属性值为 indoor
。
assert(
$('label:contains("Indoor") > input[type="radio"]').filter("[value='indoor']")
.length > 0
);
应有一个单选按钮的 value
属性值为 outdoor
。
assert(
$('label:contains("Outdoor") > input[type="radio"]').filter(
"[value='outdoor']"
).length > 0
);
应有一个复选框的 value
属性值为 loving
。
assert(
$('label:contains("Loving") > input[type="checkbox"]').filter(
"[value='loving']"
).length > 0
);
应有一个复选框的 value
属性值为 lazy
。
assert(
$('label:contains("Lazy") > input[type="checkbox"]').filter("[value='lazy']")
.length > 0
);
应有一个复选框的 value
属性值为 energetic
。
assert(
$('label:contains("Energetic") > input[type="checkbox"]').filter(
"[value='energetic']"
).length > 0
);
--seed--
--seed-contents--
<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--
<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>