尝试使用javascript制作订单表单,无法计算出计算结果?

时间:2017-04-24 22:27:21

标签: javascript html

我必须订购一份订单,我的风格很好,我无法让任何数学工作。这是我到目前为止所拥有的。不确定是否存在一些小错误,或者我是否应该重新开始。

<script>
  function $(id)
  {
    return document.getElementById(id);
  }

  function compute()
  {
    var message = "";                                 // Lists errors if any. Empty implies no errors
    form1.inpApple.style.backgroundColor = "white";   // Background 
    form1.inpPear.style.backgroundColor = "white";    //  color back 
    form1.inpGrape.style.backgroundColor = "white";   //    to white
    $("spnApple").style.display = "none";             // Red asterisk
    $("spnPear").style.display = "none";              //  display set
    $("spnGrape").style.display = "none";             //   to none

    // Get input values and validate them:
    //  Blank input will use a value of 1.
    //  Valid values will be parsed and invalid values
    //  will be flagged (background color and red *.
    var apple = form1.inpApple.value;
    if (apple == "")                                        // Test for empty string
      apple = 0;                                            // Use 1 if empty
    else
      a = parseInt(apple);                                      // Parse otherwise
    if (  isNaN(apple)  )                                   // If NaN show error
    {
      message = message + "apple bad\n";                    // Add to error list
      form1.inpApple.style.backgroundColor = "pink";  // Box now pink
      form1.inpApple.select();                        // Cursor back to bad box
      $("spnApple").style.display = "inline";         // Red * shows
    }

    var pear  = form1.inpPear.value;
    if (pear == "")                                         // Test for empty string
      pear = 0;                                             // Use 1 if empty
    else
      width = parseInt(pear);                               // Parse otherwise
    if (  isNaN(pear)  )                                    // If NaN
    {
      message = message + "pear bad\n";                     // Add to error list
      form1.inpPear.style.backgroundColor = "pink";     // Box now pink
      form1.inpPear.select();                           // Cursor back to bad box
      $("spnPear").style.display = "inline";            // Red * shows
    }

    var grape = form1.inpGrape.value;
    if (grape == "")                                        // Test for empty string
      grape = 0;                                            // Use 1 if empty
    else
      grape = parseInt(grape);                                      // Parse otherwise
    if (  isNaN(grape)  )                                   // If NaN
    {
      message = message + grape bad\n";                     // Add to error list
      form1.inpGrape.style.backgroundColor = "pink";  // Box now pink
      form1.inpGrape.select();                        // Cursor back to bad box
      $("inpGrape").style.display = "inline";         // Red * shows
    }
            {
            var count = apple + pear + g;
            form1.count.value = count.toFixed();
            }
    if (message == "") 

    { // No errors; message empty        
                var subA = 5.49 * inpApple;
                form1.subApple.value = subApple.toFixed();
                var subP = 7.49 * inpPear;
                form1.subPear.value = subPear.toFixed();
                var subG = 6.49 * inpGrape;
                form1.subGrape.value = subGrape.toFixed();
                var subT = subApple + subPear + subGrape();
                form1.subTotal.value = subTotal.toFixed();
                var subtax = subTotal * .06;
                form1.tax.value = tax.toFixed();
                var totalP = subTotal + tax;
                form1.total.value = total.toFixed();
    }
    else 
    {  // Have errors
      alert(message);                        // Show list of errors
      form1.inpApple.value = "";             // Clear result boxes on form
                form1.inpPear.value = "";
                form1.inpGrape.value = "";
                form1.subApple.value = "";
                form1.subPear.value = "";
                form1.subGrape.value = "";
                form1.count.value = "";
                form1.subTotal.value = "";
                form1.tax.value = "";
                form1.total.value = "";
    }
  }
 </script>

0 个答案:

没有答案