combobox的onSelect三级联动input用法
时间: 2024-04-30 12:20:58 浏览: 9
三级联动是指在下拉框中选择一个选项后,另外两个下拉框中的选项将会发生变化。在三级联动中,我们需要用到 `onSelect` 事件和 `input` 标签。
首先,我们需要在 HTML 中定义三个 `select` 标签,分别表示三个下拉框。然后,为每个下拉框绑定 `onchange` 事件,当下拉框选项发生变化时,触发该事件。在 `onchange` 事件中,我们可以获取当前选中的选项,并根据选项值更新其他下拉框的选项。
以下是一个简单的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>三级联动</title>
<meta charset="UTF-8">
<script type="text/javascript">
function updateSecond() {
// 获取一级下拉框选中的值
var first = document.getElementById("first").value;
// 根据一级选项更新二级下拉框选项
var second = document.getElementById("second");
second.options.length = 0;
if (first == "a") {
second.options.add(new Option("A1", "a1"));
second.options.add(new Option("A2", "a2"));
} else if (first == "b") {
second.options.add(new Option("B1", "b1"));
second.options.add(new Option("B2", "b2"));
}
// 更新三级下拉框选项
updateThird();
}
function updateThird() {
// 获取二级下拉框选中的值
var second = document.getElementById("second").value;
// 根据二级选项更新三级下拉框选项
var third = document.getElementById("third");
third.options.length = 0;
if (second == "a1") {
third.options.add(new Option("A1-1", "a1-1"));
third.options.add(new Option("A1-2", "a1-2"));
} else if (second == "a2") {
third.options.add(new Option("A2-1", "a2-1"));
third.options.add(new Option("A2-2", "a2-2"));
} else if (second == "b1") {
third.options.add(new Option("B1-1", "b1-1"));
third.options.add(new Option("B1-2", "b1-2"));
} else if (second == "b2") {
third.options.add(new Option("B2-1", "b2-1"));
third.options.add(new Option("B2-2", "b2-2"));
}
}
</script>
</head>
<body>
<select id="first" onchange="updateSecond()">
<option value="a">A</option>
<option value="b">B</option>
</select>
<select id="second" onchange="updateThird()"></select>
<select id="third"></select>
</body>
</html>
```
在上面的代码中,我们定义了三个下拉框:一级下拉框(id 为 `first`)、二级下拉框(id 为 `second`)和三级下拉框(id 为 `third`)。当一级下拉框的选项发生变化时,会触发 `updateSecond()` 函数,该函数会根据一级选项的值更新二级下拉框的选项,然后调用 `updateThird()` 函数更新三级下拉框的选项。当二级下拉框的选项发生变化时,会触发 `updateThird()` 函数,该函数会根据二级选项的值更新三级下拉框的选项。
注意,在更新下拉框选项时,我们使用了 `options.add()` 方法添加选项,该方法的第一个参数表示选项的显示文本,第二个参数表示选项的值。同时,我们还使用了 `options.length` 属性来清空下拉框之前的选项。