我可以在交换机内放置一个开关吗?

时间:2014-02-13 17:23:34

标签: javascript switch-statement

我有上课的任务,我非常困惑。我自己做了完整的任务,但这一点已经让我了。

基本上我有它所以你点击第一个艺术家,然后跟随到场地(取决于艺术家),然后根据它流向日期的地点,然后根据它流向票据金额的日期和票价。

到目前为止,我有这个:

function fillVenue() {

    //retrieves index of selected artist and target element to be populated
    var artist  = document.getElementById("artist");
    var venue   = document.getElementById("venue");
    var date    = document.getElementById("date");
    var ticket  = document.getElementById("tickets");
    var cost    = document.getElementById("cost");

    // clears data from each category
    venue.options.length = 0;
    date.options.length = 0;
    ticket.options.length = 0;
    cost.options.length = 0;

    // clearing event
    venue.onchange = null;

    // Collect and Calculate Total
    function costTotal() {
        if (ticket.selectedIndex != 0 && cost.selectedIndex != 0) {
            var costTotal       = document.getElementById("costTotal");
            var ticketCount     = ticket.value;
            var costAmount      = (cost.value).substr(1);
            costTotal.value = " £" + ticketCount * costAmount;
        }
    }
    ticket.onchange = costTotal;
    cost.onchange   = costTotal;


    switch (artist.selectedIndex) {
        case 0:
        // list begins
        // allows user to select venue
            var venueList   = ["Select Venue"]; 

        // allows user to select date
            var dateList    = ["Select Date"];

        // allows user to select ticket amount      
            var ticketList  = ["Select Tickets"];

        // allows user to select price of ticket
            var costList    = ["Select Price"];

        // fills each category  
            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);
            break;

        case 1:
            // madonna

        // allows user to select venue
            var venueList   = ["Select Venue", "London"];

         // allows user to select date
         var dateList   = ["Select Date", "17th July", "18th July"];

         // allows user to select ticket amount
        var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];

        // allows user to select price of ticket
            var costList    = ["Select Price", "£30", "£45", "£70"];

        // fills each category
            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);
            break;

        case 2:
            //Rod Stewart


        // allows user to select venue
         var venueList  = ["Select Venue", "Manchester", "Glasgow"];

        // allows user to select date
         var dateList   = ["Select Date"]

        // allows user to select ticket amount
         var ticketList = ["Select Tickets", "1", "2", "3", "4", "5", "6"];

        // allows user to select price of ticket 
         var costList   = ["Select Price", "£30", "£45", "£70"];

         //fills each category
            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);

            //  onchange event - selected Rod Stewart
            venue.onchange = function () {
                var dateList;
                switch(venue.selectedIndex) {
                    case 0: dateList = ["Select Date"]; break;
                    case 1: dateList = ["Select Date", "18th July", "20th July"]; break;
                    case 2: dateList = ["Select Date", "22nd July", "23rd July"]; break;
                }
                fillList(date, dateList);
            }
            break;

        case 3:
            //Guns and Roses
            var venueList   = ["Select Venue", "London"];
            var dateList    = ["Select Date", "10th July"];
            var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
            var costList    = ["Select Price", "£88"];
            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);
            break;

        case 4:
            // Oasis
            var venueList   = ["Select Venue", "London", "Glasgow", "Nottingham"];
            var dateList    = ["Select Date"];
            var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
            var costList    = ["Select Price"];

            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);


            venue.onchange = function  () {
                var dateList;
                switch(venue.selectedIndex) {
                    case 0: dateList = ["Select Date"]; break;
                    case 1: dateList = ["Select Date", "23rd July", "24th July"]; break ;
                    case 2: dateList = ["Select Date", "21st July"]; break;
                    case 3: dateList = ["Select Date", "18th July", "19th July"]; break;
                }

            cost.onchange = function  () {
                var costList;
                switch(cost) {
                    case 0: costList = ["Select Price"]; 
                    case 1: costList = ["Select Price", "£45", "£60"];
                    case 2: costList = ["Select Price", "£45", "£65"]; 
                    case 3: costList = ["Select Price", "£25", "£45", "£65"]; 
                }
            }
                fillList(date, dateList);
                fillList(cost, costList);
            }
            break;


        case 5:
            //Beyonce
            var venueList   = ["Select Venue", "Glasgow", "Manchester", "Birmingham", "London"];
            var dateList    = ["Select Date"];
            var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
            var costList    = ["Select Price"];
            fillList(venue, venueList);
            fillList(date, dateList);
            fillList(ticket, ticketList);
            fillList(cost, costList);
            break;
    }
}

function fillList(list,items) {
    list.options.length = 0;
    for (var i = 0; i < items.length; i++) {
        var option = new Option(items[i]);
        list.options[i] = option;
    }   
}

他们完成代码工作直至'Oasis'下拉列表。日期和地点只是不加起来。事实上,价格只是保持在“选择价格”,没有价格出现。

我正在考虑添加一个IF语句,但我不太确定这是否a)可能b)一个好主意。

提前致谢。

1 个答案:

答案 0 :(得分:0)

a)可以将开关置于开关内。为什么不应该呢?

switch (something) {
    case 1:
        switch (something else) {
             case "A":
                 // do it
                 break;
             case "B":
                 // do other things
                 break;
             default:
                 // maybe do this or that
                 break;
        }
        break;
    case 2:
        // anything else
        break
    default: 
        // whatever
        break
}

b)if内的case也是可能的。

c)你的完整结构似乎错了......在每种情况下,你似乎都做不同的事情,不同的参数也是如此。如果是这样,请不要使用switch / case或if / else,但要创建一个获取参数并相应操作的函数。它被称为DRY:&#34;不要重复自己&#34;。不要写任何代码两次或三次。如果你需要,这显然是属于某个功能的代码。

以其他格式存储该数据。例如,多维数组。 JavaScript对象。或者尽可能使用数据库 - 这毕竟是他们所依赖的。