如何将三个下拉框中选中的值自动填充到文本框内?详见代码
发布网友
发布时间:2022-04-19 06:51
我来回答
共1个回答
热心网友
时间:2022-04-19 08:20
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<body>
PM设备:
<select style="width: 100px;" id="pre" onchange="chg(this);">
<option value="-1">请选择</option>
</select>
PM类型:
<select style="width: 100px;" id="city" onchange="chg2(this)" ;></select>
PM项目:
<select style="width: 100px;" id="area"></select>
</body>
<script>
var pres = ["设备1", "设备2", "设备3"];
var cities = [
["月度PM", "季度PM", "年度PM"],
["月度PM", "季度PM", "年度PM"],
["月度PM", "季度PM", "年度PM"]
];
var areas = [
[
["1-项目1"],
["1-项目2"],
["1-项目3"]
],
[
["2-项目1"],
["2-项目2"],
["2-项目3"]
],
[
["3-项目1"],
["3-项目2"],
["3-项目3"]
]
]
var pIndex = -1;
var preEle = document.getElementById("pre");
var cityEle = document.getElementById("city");
var areaEle = document.getElementById("area");
for (var i = 0; i < pres.length; i++) {
//声明option.<option value="pres[i]">Pres[i]</option>
var op = new Option(pres[i], i);
preEle.options.add(op);
}
function chg(obj) {
if (obj.value == -1) {
cityEle.options.length = 0;
areaEle.options.length = 0;
}
var val = obj.value;
pIndex = obj.value;
var cs = cities[val];
var as = areas[val][0];
cityEle.options.length = 0;
areaEle.options.length = 0;
for (var i = 0; i < cs.length; i++) {
var op = new Option(cs[i], i);
cityEle.options.add(op);
}
for (var i = 0; i < as.length; i++) {
var op = new Option(as[i], i);
areaEle.options.add(op);
}
// alert( preEle.options[preEle.selectedIndex].text)
document.getElementById("t1").value= preEle.options[preEle.selectedIndex].text+ cityEle.options[cityEle.selectedIndex].text+ areaEle.options[areaEle.selectedIndex].text;
}
function chg2(obj) {
var val = obj.selectedIndex;
var as = areas[pIndex][val];
areaEle.options.length = 0;
for (var i = 0; i < as.length; i++) {
var op = new Option(as[i], i);
areaEle.options.add(op);
}
}
function messageReveal() {
var messageindex = document.messageForm.messagePick.selectedIndex
document.messageForm.messageField.value = messages[messageindex];
}
</script>
<input type="text" id="t1" name="t1" value="">
</head>
</html>