87 lines
2.2 KiB
Markdown
87 lines
2.2 KiB
Markdown
---
|
||
id: bad87fee1348bd9aedc08830
|
||
title: 給表單添加一個必填字段
|
||
challengeType: 0
|
||
videoUrl: 'https://scrimba.com/p/pVMPUv/cMd4EcQ'
|
||
forumTopicId: 18360
|
||
dashedName: use-html5-to-require-a-field
|
||
---
|
||
|
||
# --description--
|
||
|
||
當你設計表單時,你可以指定某些字段爲必填項(required),只有當用戶填寫了該字段後,纔可以提交表單。
|
||
|
||
如果你想把文本輸入框設置爲必填項,在 `input` 元素中加上 `required` 屬性就可以了,例如:`<input type="text" required>`
|
||
|
||
# --instructions--
|
||
|
||
請給 `input` 元素加上 `required` 屬性,這樣用戶就必須先在輸入框裏填入內容,然後纔可以提交表單。
|
||
|
||
然後嘗試在不輸入任何文本的情況下提交表單, 看看 HTML5 表單是如何通知你這個字段是必填的。
|
||
|
||
# --hints--
|
||
|
||
`input` 元素應有 `required` 屬性。
|
||
|
||
```js
|
||
assert($('input').prop('required'));
|
||
```
|
||
|
||
# --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://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" 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://www.freecatphotoapp.com/submit-cat-photo">
|
||
<input type="text" placeholder="cat photo URL">
|
||
<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://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" 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://www.freecatphotoapp.com/submit-cat-photo">
|
||
<input type="text" required placeholder="cat photo URL">
|
||
<button type="submit">Submit</button>
|
||
</form>
|
||
</main>
|
||
```
|