gpt4 book ai didi

javascript - 填充 3 个动态下拉菜单

转载 作者:行者123 更新时间:2023-11-28 03:16:31 25 4
gpt4 key购买 nike

我有 3 个动态下拉列表,它们都是从前面的下拉列表中填充的。我的问题是当我编写第三个下拉菜单时,它会干扰我的第二个下拉菜单,因此只有我的第一个下拉菜单有效。这可能很简单,但我的编程技能非常有限。

这是我的代码:

<script type="text/javascript">
function populate(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
if(s1.value == "Product1"){
var optionArray = ["|","50|50","80|80","100|100","120|120","150|150","200|200","250|250","300|300"];
} else if(s1.value == "Product2"){
var optionArray = ["|","50|50","80|80","100|100","120|120","150|150","200|200"];
} else if(s1.value == "Product3"){
var optionArray = ["|","50|50","80|80","100|100","120|120","150|150","200|200","250|250","300|300"];
} else if(s1.value == "Product4"){
var optionArray = ["|","50|50","80|80","100|100","120|120","150|150","200|200"];
}
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
}
function populate(s1,s2,s3){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
var s3 = document.getElementById(s3);
s3.innerHTML = "";
if(s1.value == ("Product1") &s2.value == ("50")) {
var optionArray = ["|","100|100","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("80")) {
var optionArray = ["|","100|100","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("100")) {
var optionArray = ["|","100|100","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("120")) {
var optionArray = ["|","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("150")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("200")) {
var optionArray = ["|","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("250")) {
var optionArray = ["|","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product1") &s2.value ==("300")) {
var optionArray = ["|","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("50")) {
var optionArray = ["|","100|100","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("80")) {
var optionArray = ["|","100|100","120|120","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("100")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("120")) {
var optionArray = ["|","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("150")) {
var optionArray = ["|","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product2") &s2.value ==("200")) {
var optionArray = ["|","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("50")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("80")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("100")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("120")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("150")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("200")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("200")) {
var optionArray = ["|","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("250")) {
var optionArray = ["|","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("250")) {
var optionArray = ["|","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product3") &s2.value ==("300")) {
var optionArray = ["|","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("50")) {
var optionArray = ["|","150|150","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("580")) {

var optionArray = ["|","200|200","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("100")) {
var optionArray = ["|","250|250","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("120")) {
var optionArray = ["|","300|300","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("150")) {
var optionArray = ["|","350|350","400|400","450|450"];
} else if(s1.value == ("Product4") &s2.value ==("200")) {
var optionArray = ["|","450|450"];
}
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s3.options.add(newOption);
}
}
</script>
<body>
<h2>Choose Your Product</h2>
<hr />
Choose Product:
<select id="slct1" name="slct1" onchange="populate(this.id,'slct2')">
<option value=""></option>
<option value="Product1">Product1</option>
<option value="Product2">Product2</option>
<option value="Product3">Product3</option>
<option value="Product4">Product4</option>
</select>
<hr />
Choose Depth:
<select id="slct2" name="slct2" onchange="populate('slct2' 'slct1','slct3')"></select>
<hr />
Choose Height:
<select id="slct3" name="slct3"></select>
<hr />
</body>

最佳答案

你有两个主要的语法错误:

1- 你有两个同名的函数!填充。

2- 第二个列表中函数参数的调用是错误的 populate('slct2' 'slct1','slct3')" 应该有一个 ,在 slct2 和 slct1 之间。

查看此演示:http://jsbin.com/xameye/1/

关于javascript - 填充 3 个动态下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com