我不知道如何正确地关联按钮

时间:2014-11-22 01:20:49

标签: javascript

你好,我很新,我不知道做这件事我需要知道的所有事情。我需要按钮来关联与类型相对应的门票销售,我希望它在达到可销售的销售数量后终止。我已经为我知道如何做到的所有内容编写了代码,所以如果遗漏了它可能会导致我是一个不知道我在做什么的白痴

<div align="center">
    <div>Hello, and welcome to the Porpoise Pond Virtual Ticket Booth. We are running a special so toddlers get in for free!</div>
    <div>This menu is here to simplify ticket sales. It will automatically stop when the ticket sales when the seats are sold out</div>
    <div>Be aware that the maxium amount of ticket that can be purchased in one transaction is 10!</div>
    <div>Please press the button with the type of ticket you wish to purchase  to start ordering your tickets now.</div>
      <button onclick="Calculate();">Toddler Ticket</button>
    <button onclick="Calculate();">Junior Ticket</button>
    <button onclick="Calculate();">Adult Ticket</button>
    <div id='msg'></div>

</head>

<body>
    <script type="text/javascript">
        // Program name: Ticket Booth
        // Purpose: To do a transaction
        // Author: Ephraim Vickers
        // Date last modified: Today

        // Variables
            var MAX_SEAT_COUNT = 10             //Named constant
            var amountTicket                    //Amount of tickets bought
            var grossProfit
                    //Ticket prices
            var adulTicket = 0           
            var junTicket = 0
            var todTicket = 0
            var totalSales = 0                  // adulTicket + junTicket 
                    //Seats by class
            var adulNum = 0                     // number of tickets bought
            var junNum = 0
            var todNum = 0
            var extraNum = 0                    //Ticket bought over maximum

        function parseFloat(amountTicket) {

            if (amountTicket >= MAX_SEAT_COUNT) {
                extraNum = amountTicket - MAX_SEAT_COUNT;
                document.getElementById('msg').innerHTML = "Tickets have now sold out thank you for purchasing."  // important to stop after limit
                 //end loop and display all data here like shown below remember to line up columns
                 //Ticket sale Report  <current date>
  // SEAT CAPACITY   EXTRAS            TOTAL SOLD
  // MAX_SEAT_COUNT  extraNum          amountTicket

  //  TODDLERS    JUNIORS               ADULTS
  //  todNum      junNum                  adulNum 
  //Gross Profit for today was grossProfit

              }else if    (amountTicket > 10) {
                 document.getElementById('msg').innerHTML = "The maximum number of tickets that can be done in one transaction is 10"
              }else {
                   amountTicket = prompt("Valued customer, please enter the number of tickets you would like to purchase.");
            parseFloat(amountTicket);
              }
              return;
              }
    function Calculate() {
        //set zeroes for all values
        adulTicket = 0
        junTicket = 0
        todTicket = 0
        amountTicket = 0
        totalSales = 0
        adulNum = 0
        junNum = 0
        todNUm = 0
        extraNum = 0
        grossProfit = 0
       //get user input
            amountTicket = prompt("Valued customer, please enter the number of tickets you would like to purchase.");
            parseFloat(amountTicket);
        grossProfit =  (adulTicket * adultNum) + (junTicket * junNum)

    }

    </script>

1 个答案:

答案 0 :(得分:1)

您可以将id变量传递给Calculate函数,如:

<button onclick="Calculate(0);">Toddler Ticket</button>
<button onclick="Calculate(1);">Junior Ticket</button>
<button onclick="Calculate(2);">Adult Ticket</button>

然后您可以使用此ID来识别按下的按钮:

function Calculate(id) {
        //set zeroes for all values
        adulTicket = 0
        junTicket = 0
        todTicket = 0
        amountTicket = 0
        totalSales = 0
        adulNum = 0
        junNum = 0
        todNUm = 0
        extraNum = 0
        grossProfit = 0
        switch(id){
        case 0:
        //code for toddler
        break;
        case 1:
        //code  for junior
        break;
        case 2:
        // code for adult
        break;
        }
       //get user input
            amountTicket = prompt("Valued customer, please enter the number of tickets you would like to purchase.");
            parseFloat(amountTicket);
        grossProfit =  (adulTicket * adultNum) + (junTicket * junNum)

    }

我希望这适合你。