填充3个动态下拉列表

时间:2014-12-09 13:51:03

标签: javascript html onchange

我有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>
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

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

1-你有两个同名的功能!填充。

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

结帐本次演示:http://jsbin.com/xameye/1/

答案 1 :(得分:0)

您的代码中存在很少的语法错误,而且您使用这么多静态值来填充下拉选项,请您纠正语法错误并简化代码,之后您将能够看到正确的结果或者确切的错误。