这段代码可以工作(我知道这是使用开关的错误方式),但我似乎是以错误的方式来处理这个问题。我有两个下拉菜单框在我的网页与价值,可以混合任何方式。
例如,每个框中有两个值:
但是它可能有20个值,所以代码会增长得相当快。我必须知道用户在两个框和它们的组合中选择了什么,因为它将显示关于两个框和它们的混合的数据。
所以简单地说,我的问题是,有什么最佳的解决办法吗?我希望页面加载速度快,易于维护。
function solveit() {
//These are dropdownlist that will just send and index or value as text
var selectedIndex1 = document.getElementById("selectedIndex1").value;
var selectedIndex2 = document.getElementById("selectedIndex2").value;
switch (selectedIndex1 + "|" + selectedIndex2){
case "1|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "1|2":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "1|3":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "1|4":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "1|5":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "2|1":
document.getElementById("IndexANDIndex").innerHTML = "Info about the two values"
break;
case "2|2":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "2|3":
document.getElementById("IndexANDIndex").innerHTML = "Info about the two values"
break;
case "2|4":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "2|5":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "3|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "3|2":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "3|3":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "3|4":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "3|5":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
//And so on...
case "1|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "2|1":
document.getElementById("IndexANDIndex").innerHTML = "Info about the two values"
break;
case "3|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "4|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
case "5|1":
document.getElementById("IndexANDIndex").innerHTML = "Information about the combination of them"
break;
//And so on...
default:
document.getElementById("IndexANDIndex").innerHTML = "NOTHING CHOOSEN"}}
发布于 2016-03-02 21:17:15
我就是这样做的。因此,请更改拖放框中的值。就像1,2,4,8,16,32,64.诸若此类。
所以item1和item1是1+1 =2,item2和item1是2+1 = 3。这样,我就不用费心处理1\1.
也许不是最好的解决办法,但我现在对此很满意。
发布于 2016-01-17 19:24:37
我们可以用更少的开销编写相同的代码:
function solveit() {
//These are dropdownlist that will just send and index or value as text
var selectedIndex1 = document.getElementById("selectedIndex1").value;
var selectedIndex2 = document.getElementById("selectedIndex2").value;
document.getElementById("IndexANDIndex").innerHTML = getInfo(selectedIndex1 + "|" + selectedIndex2);
}
function getInfo(sel) {
switch (sel){
case "1|1": return "Information about the combination of them";
case "1|2": return "Information about the combination of them";
case "1|3": return "Information about the combination of them";
case "1|4": return "Information about the combination of them";
case "1|5": return "Information about the combination of them";
case "2|1": return "Info about the two values";
case "2|2": return "Information about the combination of them";
case "2|3": return "Info about the two values";
case "2|4": return "Information about the combination of them";
case "2|5": return "Information about the combination of them";
case "3|1": return "Information about the combination of them";
case "3|2": return "Information about the combination of them";
case "3|3": return "Information about the combination of them";
case "3|4": return "Information about the combination of them";
case "3|5": return "Information about the combination of them";
//And so on...
}
return "NOTHING CHOOSEN";
}https://stackoverflow.com/questions/34747688
复制相似问题