| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: bad87fee1348bd9aedc08830 | 
					
						
							|  |  |  | title: Use HTML5 to Require a Field | 
					
						
							|  |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | videoUrl: 'https://scrimba.com/p/pVMPUv/cMd4EcQ' | 
					
						
							|  |  |  | forumTopicId: 18360 | 
					
						
							|  |  |  | localeTitle: 给表单添加一个必填字段 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Description
 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | <section id='description'> | 
					
						
							|  |  |  | 当你设计表单时,你可以指定某些字段为必填项(required),只有当用户填写了该字段后,才可以提交表单。 | 
					
						
							|  |  |  | 如果你想把文本输入框设置为必填项,在<code>input</code>元素中加上 required 属性就可以了,例如:<code><input type="text" required></code> | 
					
						
							|  |  |  | </section> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Instructions
 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | <section id='instructions'> | 
					
						
							|  |  |  | 给<code>input</code>元素加上<code>required</code>属性,这样用户就必须先在输入框里填入内容,然后才可以提交表单。 | 
					
						
							|  |  |  | </section> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Tests
 | 
					
						
							|  |  |  | <section id='tests'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```yml | 
					
						
							|  |  |  | tests: | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |   - text: '<code>input</code>元素必须有<code>required</code>属性。' | 
					
						
							|  |  |  |     testString: assert($("input").prop("required")); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Challenge Seed
 | 
					
						
							|  |  |  | <section id='challengeSeed'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div id='html-seed'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <h2>CatPhotoApp</h2> | 
					
						
							|  |  |  | <main> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | <p>点击查看更多<a href="#">猫咪图片</a>。</p> | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   <a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="一只仰卧着的萌猫"></a> | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   <p>猫咪最喜欢的三件东西:</p> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |   <ul> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |     <li>猫薄荷</li> | 
					
						
							|  |  |  |     <li>激光笔</li> | 
					
						
							|  |  |  |     <li>千层饼</li> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |   </ul> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |   <p>猫咪最讨厌的三件东西:</p> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |   <ol> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |     <li>跳蚤</li> | 
					
						
							|  |  |  |     <li>打雷</li> | 
					
						
							|  |  |  |     <li>同类</li> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |   </ol> | 
					
						
							| 
									
										
										
										
											2020-07-15 02:55:06 -07:00
										 |  |  |   <form action="https://freecatphotoapp.com/submit-cat-photo"> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |     <input type="text" placeholder="猫咪图片地址"> | 
					
						
							|  |  |  |     <button type="submit">提交</button> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |   </form> | 
					
						
							|  |  |  | </main> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Solution
 | 
					
						
							|  |  |  | <section id='solution'> | 
					
						
							|  |  |  | </section> | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  |                |