| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5c6c06847491271903d37cfd | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | title: 使用单选框和复选框的 value 属性 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | challengeType: 0 | 
					
						
							|  |  |  | forumTopicId: 301099 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: use-the-value-attribute-with-radio-buttons-and-checkboxes | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 提交表单时,所选项的值会发送给服务端。 `radio` 和 `checkbox` 的 `value` 属性值决定了发送到服务端的实际内容。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 例如: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | <label for="indoor"> | 
					
						
							|  |  |  |   <input id="indoor" value="indoor" type="radio" name="indoor-outdoor">Indoor | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | </label> | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | <label for="outdoor"> | 
					
						
							|  |  |  |   <input id="outdoor" value="outdoor" type="radio" name="indoor-outdoor">Outdoor | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | </label> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 这里有两个 `radio` 单选框。 当用户提交表单时,如果 `indoor` 选项被选中,表单数据会包含:`indoor-outdoor=indoor`。 也就是所选项的 `name` 和 `value` 属性值。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 如果没有指明 `value` 属性值,则会使用默认值做为表单数据提交,也就是 `on`。 在这种情况下,如果用户选中 "indoor" 选项然后提交表单,表单数据则会包含 `indoor-outdoor=on`。 这样的表单数据看起来不够直观,因此最好将 `value` 属性值设置为一些有意义的内容。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 给每一个 `radio` 和 `checkbox` 输入框添加 `value` 属性。 使用输入标签的文本,小写形式,作为属性的值。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 应有一个单选按钮的 `value` 属性值为 `indoor`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   $('label:contains("Indoor") > input[type="radio"]').filter("[value='indoor']") | 
					
						
							|  |  |  |     .length > 0 | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 应有一个单选按钮的 `value` 属性值为 `outdoor`。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   $('label:contains("Outdoor") > input[type="radio"]').filter( | 
					
						
							|  |  |  |     "[value='outdoor']" | 
					
						
							|  |  |  |   ).length > 0 | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 应有一个复选框的 `value` 属性值为 `loving`。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   $('label:contains("Loving") > input[type="checkbox"]').filter( | 
					
						
							|  |  |  |     "[value='loving']" | 
					
						
							|  |  |  |   ).length > 0 | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 应有一个复选框的 `value` 属性值为 `lazy`。 | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   $('label:contains("Lazy") > input[type="checkbox"]').filter("[value='lazy']") | 
					
						
							|  |  |  |     .length > 0 | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2019-12-26 20:05:59 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 应有一个复选框的 `value` 属性值为 `energetic`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   $('label:contains("Energetic") > input[type="checkbox"]').filter( | 
					
						
							|  |  |  |     "[value='energetic']" | 
					
						
							|  |  |  |   ).length > 0 | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-28 00:16:44 +09:00
										 |  |  |   <a href="#"><img src="https://www.bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a> | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   <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> | 
					
						
							| 
									
										
										
										
											2021-04-28 00:16:44 +09:00
										 |  |  |   <form action="https://www.freecatphotoapp.com/submit-cat-photo"> | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |     <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> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-28 00:16:44 +09:00
										 |  |  |   <a href="#"><img src="https://www.bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a> | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   <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> | 
					
						
							| 
									
										
										
										
											2021-04-28 00:16:44 +09:00
										 |  |  |   <form action="https://www.freecatphotoapp.com/submit-cat-photo"> | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |     <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> | 
					
						
							|  |  |  | ``` |