0

我正在制作一个价目表,我有很多数据要解析。所有的数据都是不同的,没有公式。下面是我正在使用的一个片段。我想知道是否有更好的方法来存储和访问所有这些数据。

    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;
4

1 回答 1

1

重新排列一些代码可能会有所帮助。例如,该document.getElementById(ipp).style.border行在所有块中都是相同的,除了第一个。因此,您可以将其取出并单独制作一个块:

document.getElementById(ipp).style.border = cabs.value ? "" : "1px solid red";

然后看起来你的b1出租车可以这样安排:

cabdata = [111,113,116,118,120,122,124,126,141,146,146,152,152];
// note duplicate values at end due to bigger interval
cabindex = Math.max(0,Math.ceil((cabs.value-12)/1.5));
totalPrice += cabdata[cabindex];
document.getElementById(cabP).innerHTML = " $"+cabdata[cabindex];

同样对于b2

cabdata = [167,173,178,183,194,199,205];
cabindex = Math.max(0,Math.ceil((cabs.value-27)/3));
totalPrice += cabdata[cabindex];
document.getElementById(cabP).innerHTML = " $"+cabdata[cabindex];

另请注意,上述两个块中的最后两行相同,因此可以将它们移动到switch.

总有一个公式,尽管它可能不是那么简单。示例:蝙蝠侠公式

于 2013-02-27T21:54:22.157 回答