2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedc08830
|
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/cMd4EcQ'
|
|
|
|
|
forumTopicId: 18360
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-html5-to-require-a-field
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
当你设计表单时,你可以指定某些字段为必填项(required),只有当用户填写了该字段后,才可以提交表单。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
如果你想把文本输入框设置为必填项,在 `input` 元素中加上 `required` 属性就可以了,例如:`<input type="text" required>`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
请给 `input` 元素加上 `required` 属性,这样用户就必须先在输入框里填入内容,然后才可以提交表单。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
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
|
|
|
|
`input` 元素应有 `required` 属性。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('input').prop('required'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --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">
|
|
|
|
|
<input type="text" placeholder="cat photo URL">
|
|
|
|
|
<button type="submit">Submit</button>
|
|
|
|
|
</form>
|
|
|
|
|
</main>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```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">
|
|
|
|
|
<input type="text" required placeholder="cat photo URL">
|
|
|
|
|
<button type="submit">Submit</button>
|
|
|
|
|
</form>
|
|
|
|
|
</main>
|
|
|
|
|
```
|