我正在制定價目表,並且我有很多數據需要解析。所有的數據是不同的,並沒有公式。以下是我正在使用的一小部分內容。我想知道是否有更好的方式來存儲和訪問所有這些數據。更好的方法來存儲大量的數據
function getPrice(number) {
totalPrice = 0;
for (var t = 2; t <= number; t++) {
var ipp = 'cab' + (t - 1) + 'Size';
var cabP = 'p' + (t - 1);
var cabStyle = document.getElementById('cab' + (t - 1)).value;
var cabs = document.getElementById(ipp);
switch (cabStyle) {
case 'b1':
if (cabs.value === "") {
totalPrice = totalPrice + 0;
document.getElementById(ipp).style.border = "1px solid red";
} else if (cabs.value <= 12) {
totalPrice = totalPrice + 111;
document.getElementById(cabP).innerHTML = " $111";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 12 && cabs.value <= 13.5) {
totalPrice = totalPrice + 113;
document.getElementById(cabP).innerHTML = " $113";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 13.5 && cabs.value <= 15) {
totalPrice = totalPrice + 116;
document.getElementById(cabP).innerHTML = " $116";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 15 && cabs.value <= 16.5) {
totalPrice = totalPrice + 118;
document.getElementById(cabP).innerHTML = " $118";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 16.5 && cabs.value <= 18) {
totalPrice = totalPrice + 120;
document.getElementById(cabP).innerHTML = " $120";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 18 && cabs.value <= 19.5) {
totalPrice = totalPrice + 122;
document.getElementById(cabP).innerHTML = " $122";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 19.5 && cabs.value <= 21) {
totalPrice = totalPrice + 124;
document.getElementById(cabP).innerHTML = " $124";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 21 && cabs.value <= 22.5) {
totalPrice = totalPrice + 126;
document.getElementById(cabP).innerHTML = " $126";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 22.5 && cabs.value <= 24) {
totalPrice = totalPrice + 141;
document.getElementById(cabP).innerHTML = " $141";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 24 && cabs.value <= 27) {
totalPrice = totalPrice + 146;
document.getElementById(cabP).innerHTML = " $146";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 27 && cabs.value <= 30) {
totalPrice = totalPrice + 152;
document.getElementById(cabP).innerHTML = " $152";
document.getElementById(ipp).style.border = "";
}
break;
case 'b2':
if (cabs.value === "") {
totalPrice = totalPrice + 0;
document.getElementById(ipp).style.border = "1px solid red";
} else if (cabs.value <= 27) {
totalPrice = totalPrice + 167;
document.getElementById(cabP).innerHTML = " $167";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 27 && cabs.value <= 30) {
totalPrice = totalPrice + 173;
document.getElementById(cabP).innerHTML = " $173";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 30 && cabs.value <= 33) {
totalPrice = totalPrice + 178;
document.getElementById(cabP).innerHTML = " $178";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 33 && cabs.value <= 36) {
totalPrice = totalPrice + 183;
document.getElementById(cabP).innerHTML = " $183";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 36 && cabs.value <= 39) {
totalPrice = totalPrice + 194;
document.getElementById(cabP).innerHTML = " $194";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 39 && cabs.value <= 42) {
totalPrice = totalPrice + 199;
document.getElementById(cabP).innerHTML = " $199";
document.getElementById(ipp).style.border = "";
} else if (cabs.value > 42 && cabs.value <= 45) {
totalPrice = totalPrice + 205;
document.getElementById(cabP).innerHTML = " $205";
document.getElementById(ipp).style.border = "";
}
break;
這是什麼情況?你是否以某種方式從HTML中提取價格? '數字'和'cabStyle'是什麼? – 2013-02-27 22:12:26
'number'只是一個按鈕功能的計數器,'cabStyle'只是選項列表中的樣式。 – punksux 2013-02-27 22:25:25