多个JavaScript函数无法正常工作

时间:2013-11-07 15:08:44

标签: javascript

我已经编写了这个基本的javascript代码但是当我合并第二个函数,即formSubmit()时,名为myfunction()的函数不起作用。但是,如果我删除第二个函数,那么函数myfunction()工作正常。请帮我解决这个问题。我不知道为什么它不起作用:(

  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="ISO-8859-1">
  <title>DuncanDonut</title>
  <script type="text/javascript">
    function myfunction() {
      var DONUTRATE = 0.5;
      var TAXRATE = 0.5 ;
      var DONUTPRICE = 50;
      var nameinput = " ";

      var num_of_donuts = document.getElementById("numOfdonuts").value;
      var num_of_glazing_donuts = document.getElementById("numOfGladonuts").value;

      if(num_of_donuts != "" && num_of_glazing_donuts!= "" ) {
        var tot_num_of_donuts =parseInt(num_of_donuts)+parseInt(num_of_glazing_donuts);
        var totTime =  tot_num_of_donuts*DONUTRATE;
        document.getElementById("time").value = totTime.toFixed(2)+" hrs";
        var subtotal = tot_num_of_donuts*DONUTPRICE;
        document.getElementById("subPrice").value = "$"+subtotal.toFixed(2);
        var tax = subtotal*TAXRATE;
        document.getElementById("tax").value = "$"+tax.toFixed(2);
        var Total = subtotal + tax;
        document.getElementById("total").value = "$"+Total.toFixed(2);
      }
    };

    function formSubmit()  {
      if(document.getElementById("username").value == "") {
        alert("Enter A Valid Name");
      } else {
        alert("WE Entered Here 3");
        form.submit();
      }
    }
  </script>
  </head>
  <body bgcolor="#C0C0C0">
  <hr size="1"width="1200">
  <form method="post" action="mySevlet.do" onsubmit= "formSubmit();">
  <table>
  <tr> 
  <td>  Name: </td>
  <td><input type="text" name="name" id="username" size="12" onchange="myfunction();"> 
  </td>
  </tr>
  <tr> 
  <td>  Number Of Donuts: </td>
  <td> <input type="text" name="totDonuts" id="numOfdonuts" size="5" onchange=    
       "myfunction();"> </td>
  </tr>
  <tr> 
  <td>  Number Of Glaze Donuts: </td>
  <td> <input type="text" name="totGlaDonuts" id="numOfGladonuts" size="5" onchange=   
      "myfunction();"> </td>
  </tr>
  <tr> 
  <td>  Time: </td>
  <td> <input type="text" name="timeNeeded" id="time" size="5" readonly> </td>
  </tr>
  <tr> 
  <td>  Subtotal: </td>
  <td> <input type="text" name="subtotal" id="subPrice" size="5" readonly> </td>
  </tr>
  <tr> 
  <td>  Tax: </td>
  <td> <input type="text" name="Taxprice" id="tax" size="5" readonly> </td>
  </tr>
  <tr> 
  <td>  Total: </td>
  <td> <input type="text" name="fullprice" id="total" size="5" readonly> </td>
  </tr>
  <tr>
  <td><input type="submit"value="submit"></td>
  </tr>
  </table>
  </form>
  <hr size="1"width="1200">
  </body>
  </html>

0 个答案:

没有答案