2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedd08835
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 给单选按钮和复选框添加默认选中项
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 0
|
2019-12-26 12:05:59 +00:00
|
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/cWk3Qh6'
|
|
|
|
forumTopicId: 301094
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
如果想设置某个单选按钮或多选按钮默认被选中,只需给`input`元素添加 "checked" 属性。 例如:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
`<input type="radio" name="test-name" checked>`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
把第一个`radio button`和第一个`checkbox`都设置为默认选中。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
表单的第一个单选按钮应该被默认选中。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('input[type="radio"]').prop('checked'));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
表单的第一个多选按钮应该被默认选中。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('input[type="checkbox"]').prop('checked'));
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|