如何在switch语句后调用函数?

时间:2017-08-25 07:29:54

标签: javascript jquery html

我正在建立一个表格。其中有一个包含9个选项的选择器,它们彼此相似但不同。

我在select中的每次更改后调用calcTotal()函数。 calcTotal有一个包含9个案例的switch语句。我想我没有正确地调用calcTotal函数。因为在我选择一个选项后,adultTotalchildTotal没有按照分配进行更新,而是将其值显示为“未定义”。

每次选择后如何调用函数? (在这种情况下我有4个,“typeSel”,“c1bed”,“c2bed”,“c3bed”)

$(document).ready(function () {
    $('#typeSel').on('change', function () {
        switch (this.value) {
            case '1': {
                $("#a1det").show();
                $("#cdet,#a2det,#a3det,#a4det").hide();
                $("#a1det :input,#adult1").attr("disabled", false);
                $("#cdet :input,#childr :input,#a2det :input,#a3det :input,#a4det :input,#adult2,#adult3,#adult4").attr("disabled", true);
                document.getElementById("booktype").value = "1";
                document.getElementById("adultNum").value = "1";
                document.getElementById("childNum").value = "0";
                booktype = 1;
                return booktype;
                break;
            }

            case '2': {
                $("#a1det, #a2det").show();
                $("#cdet,#a3det,#a4det").hide();
                $("#a1det :input,#a2det :input,#adult2").attr("disabled", false);
                $("#cdet :input,#childr :input,#a3det :input,#a4det :input,#adult3,#adult4").attr("disabled", true);
                document.getElementById("booktype").value = "2";
                document.getElementById("adultNum").value = "2";
                document.getElementById("childNum").value = "0";
                booktype = 2;
                return booktype;
                break;
            }

            case '3': {
                $("#a1det, #a2det, #a3det").show();
                $("#cdet,#a4det").hide();
                $("#a1det :input,#a2det :input,#a3det :input,#adult1,#adult2,#adult3").attr("disabled", false);
                $("#cdet :input,#childr :input,#a4det :input,#adult4").attr("disabled", true);
                document.getElementById("booktype").value = "3";
                document.getElementById("adultNum").value = "3";
                document.getElementById("childNum").value = "0";
                booktype = 3;
                return booktype;
                break;
            }

            case '4': {
                $("#a1det, #a2det, #a3det, #a4det").show();
                $("#cdet").hide();
                $("#a1det :input,#a2det :input,#a3det :input,#a4det :input,#adult1,#adult2,#adult3,#adult4").attr("disabled", false);
                $("#cdet :input,#childr :input").attr("disabled", true);
                document.getElementById("booktype").value = "4";
                document.getElementById("adultNum").value = "4";
                document.getElementById("childNum").value = "0";
                booktype = 4;
                return booktype;
                break;
            }

            case '5': {
                $("#a1det, #a2det, #cdet, #c1det").show();
                $("#a3det,#a4det,#c2det,#c3det").hide();
                $("#a1det :input,#a2det :input,#c1det :input, #child1,#adult1,#adult2,#childTotal").attr("disabled", false);
                $("#child2,#child3,#adult3,#adult4,#a3det :input,#a4det :input,#c2det :input,#c3det :input").attr("disabled", true);
                document.getElementById("booktype").value = "5";
                document.getElementById("adultNum").value = "2";
                document.getElementById("childNum").value = "1";
                booktype = 5;
                return booktype;
                break;
            }

            case '6': {
                $("#a1det, #a2det, #cdet, #c1det, #c2det").show();
                $("#a3det,#a4det,#c3det").hide();
                $("#a1det :input,#a2det :input,#c1det :input,#c2det :input,#adult1,#adult2,#child1,#child2,#childTotal").attr("disabled", false);
                $("#child3,#adult3,#adult4,#a3det :input,#a4det :input,#c3det :input").attr("disabled", true);
                document.getElementById("booktype").value = "6";
                document.getElementById("adultNum").value = "2";
                document.getElementById("childNum").value = "2";
                booktype = 6;
                return booktype;
                break;
            }

            case '7': {
                $("#a1det, #a2det, #cdet, #c1det, #c2det, #c3det").show();
                $("#a3det,#a4det").hide();
                $("#a1det :input,#a2det :input,#c1det :input,#c2det :input,#c3det :input,#adult1,#adult2,#child1,#child2,#child3,#childTotal").attr("disabled", false);
                $("#adult3,#adult4,#a3det :input,#a4det :input").attr("disabled", true);
                document.getElementById("booktype").value = "7";
                document.getElementById("adultNum").value = "2";
                document.getElementById("childNum").value = "3";
                booktype = 7;
                return booktype;
                break;
            }

            case '8': {
                $("#a1det, #a2det, #a3det, #cdet, #c1det").show();
                $("#a4det,#c2det,#c3det").hide();
                $("#a1det :input,#a2det :input,#a3det :input,#c1det :input, #child1,#adult1,#adult2,#adult3,#childTotal").attr("disabled", false);
                $("#child2,#child3,#adult4,#a4det :input,#c2det :input,#c3det :input").attr("disabled", true);
                document.getElementById("booktype").value = "8";
                document.getElementById("adultNum").value = "3";
                document.getElementById("childNum").value = "1";
                booktype = 8;
                return booktype;
                break;
            }

            case '9': {
                $("#a1det, #a2det, #a3det, #cdet, #c1det, #c2det").show();
                $("#a4det,#c3det").hide();
                $("#a1det :input,#a2det :input,#a3det :input,#c1det :input,#c2det :input,#child1,#child2,#adult1,#adult2,#adult3,#childTotal").attr("disabled", false);
                $("#child3,#adult4,#a4det :input,#c3det :input").attr("disabled", true);
                document.getElementById("booktype").value = "9";
                document.getElementById("adultNum").value = "3";
                document.getElementById("childNum").value = "2";
                booktype = 9;
                return booktype;
                break;
            }
        }
        return booktype;
        calcTotal();
    });

    $('#c1bed').on('change', function () {
        switch (this.value) {
            case 'a': {
                child1 = childa;
                break;
            }
            case 'b': {
                child1 = childb;
                break;
            }
            case 'c': {
                child1 = childc;
                break;
            }
            case 'd': {
                child1 = childd;
                break;
            }
        }
        document.getElementById("child1").value = child1;
        calcTotal();
        return child1;
    });

    $('#c2bed').on('change', function () {
        switch (this.value) {
            case 'a': {
                child2 = childa;
                break;
            }
            case 'b': {
                child2 = childb;
                break;
            }
            case 'c': {
                child2 = childc;
                break;
            }
            case 'd': {
                child2 = childd;
                break;
            }
        }
        document.getElementById("child2").value = child2;
        calcTotal();
        return child2;
    });

    $('#c3bed').on('change', function () {
        switch (this.value) {
            case 'a': {
                child3 = childa;
                break;
            }
            case 'b': {
                child3 = childb;
                break;
            }
            case 'c': {
                child3 = childc;
                break;
            }
            case 'd': {
                child3 = childd;
                break;
            }
        }
        document.getElementById("child3").value = child3;
        calcTotal();
        return child3;
    });
});

function calcTotal() {
    switch (booktype) {
        case '1': {
            adultTotal = adult1;
            childTotal = 0;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '2': {
            adultTotal = +adult1 + +adult2;
            childTotal = 0;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '3': {
            adultTotal = +adult1 + +adult2 + +adult3;
            childTotal = 0;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '4': {
            adultTotal = +adult1 + +adult2 + +adult3 + +adult4;
            childTotal = 0;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '5': {
            adultTotal = +adult1 + +adult2;
            childTotal = +child1;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '6': {
            adultTotal = +adult1 + +adult2;
            childTotal = +child1 + +child2;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '7': {
            adultTotal = +adult1 + +adult2;
            childTotal = +child1 + +child2 + +child3;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '8': {
            adultTotal = +adult1 + +adult2 + +adult3;
            childTotal = +child1;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
        case '9': {
            adultTotal = +adult1 + +adult2 + +adult3;
            childTotal = +child1 + +child2;
            document.getElementById("adultTotal").value = adultTotal;
            document.getElementById("childTotal").value = childTotal;
            break;
        }
    }
    totalFare = +adultTotal + +childTotal;
    document.getElementById("totalFare").value = totalFare;
}

1 个答案:

答案 0 :(得分:1)

我只专注于第一个开关,因为你的代码真的很乱,很难理解......

解释

calcTotal()必须 befor return。返回语句将跳出函数,其下的任何内容都不会执行。

代码

您的switch非常多余。您想要做的只是将value作为number返回。这可以通过Number(this.value)实现。为了您的回报,您必须致电calcTotal()

$('#typeSel').on('change', function() {
    calcTotal();
    return Number(this.value);
});