浏览器与 Web API / 系统课时
复选框、单选框与下拉框
复选框、单选框与下拉框
复选框、单选框和下拉框都用于表达“选择”,但它们暴露的状态并不相同。本课将用 checked、selected、selectedIndex 和 options 读取当前选择,并把多种控件状态整理成统一、可验证的结果。
学习目标
- 使用
checked读取和设置复选框、单选框状态。 - 使用
selected、selectedIndex、options处理下拉选项。 - 正确读取单选组、单选下拉和多选下拉的值。
复选框与单选框
复选框和单选框都通过布尔属性 checked 表示当前是否选中。复选框彼此独立,适合表达多个可以同时成立的条件;同名的单选框属于一个组,组内通常只允许一个控件处于选中状态。
读取单选组时,不应逐个猜测固定位置,而应查询当前满足 :checked 的元素。若没有任何项被选中,查询结果为 null,业务代码需要提供默认值或提示用户选择。
HTML 中的 checked 属性描述默认状态,DOM 的 checked 属性描述当前状态。用户操作只改变当前状态;需要读取初始状态时,可使用 defaultChecked。
下拉框的状态模型
select.selectedIndex 返回第一个选中选项的索引,没有选项时为 -1。select.options 是包含全部 option 的集合,可以按索引读取,也可以转换成数组后筛选。
每个 option.selected 表示该选项当前是否被选中。单选下拉中通常只有一个选项为 true;带有 multiple 的多选下拉可以同时选中多项。读取多选结果时,可使用 selectedOptions,也可以从 options 中筛选 selected。
select.value 适合读取单选下拉的当前值;在多选下拉中,它只返回第一个选中项的值,不能代表全部选择。
完整示例
下面的页面同时演示复选框、单选组、单选下拉和多选下拉。点击“设置示例状态”后再点击“读取状态”,可以得到固定结果。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>读取选择控件</title>
</head>
<body>
<label>
<input id="subscribe" type="checkbox" checked>
接收更新
</label>
<fieldset>
<legend>配送方式</legend>
<label><input type="radio" name="delivery" value="standard" checked>普通</label>
<label><input type="radio" name="delivery" value="express">加急</label>
</fieldset>
<label>
城市
<select id="city">
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<option value="shenzhen">深圳</option>
</select>
</label>
<label>
技能
<select id="skills" multiple>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>
</label>
<button id="preset" type="button">设置示例状态</button>
<button id="read" type="button">读取状态</button>
<pre id="output"></pre>
<script>
const subscribe = document.querySelector("#subscribe");
const city = document.querySelector("#city");
const skills = document.querySelector("#skills");
const output = document.querySelector("#output");
document.querySelector("#preset").addEventListener("click", () => {
subscribe.checked = false;
document.querySelector('input[value="express"]').checked = true;
city.selectedIndex = 2;
// 多选框先清空,再设置需要的选项。
for (const option of skills.options) {
option.selected = false;
}
skills.options[0].selected = true;
skills.options[2].selected = true;
});
document.querySelector("#read").addEventListener("click", () => {
const delivery = document.querySelector('input[name="delivery"]:checked');
const selectedSkills = Array.from(skills.options)
.filter((option) => option.selected)
.map((option) => option.value);
output.textContent = [
`订阅:${subscribe.checked ? "是" : "否"}`,
`配送:${delivery?.value ?? "未选择"}`,
`城市:${city.value}`,
`城市索引:${city.selectedIndex}`,
`城市选项数:${city.options.length}`,
`技能:${selectedSkills.join(", ")}`
].join("\n");
});
</script>
</body>
</html>按顺序点击“设置示例状态”和“读取状态”,页面预期显示:
订阅:否
配送:express
城市:shenzhen
城市索引:2
城市选项数:3
技能:html, js设置状态与边界处理
脚本可以直接给 checked、selected 或 selectedIndex 赋值。给同一单选组中的某个控件设置 checked = true 时,浏览器会自动取消该组原来的选中项。给单选下拉设置 selectedIndex 时,浏览器也会同步更新各个选项的 selected 状态。
动态删除选项、禁用选项或把 selectedIndex 设为 -1 后,下拉框可能没有有效选择。提交前不要只假设页面一定存在选中项,应检查索引、值或选中集合,并为必填控件结合约束验证提供提示。