2016-11-15 102 views
0

我正在處理從用戶和下拉數組中獲取值以計算值並將其顯示在空的表單字段中的表單。這也需要一個貨幣格式函數以美元顯示價值。除了貨幣格式之外,我可以讓所有的工作都很好。未捕獲的引用錯誤javascript

代碼現在的顯示方式是uncaught reference error。如果我從onClick事件中取消inNum函數名稱,則if語句的第一部分工作,顯示「無效金額」。

任何幫助解決這個難題非常感謝!謝謝!

function getShipping(inNum) { 
 
    var product = document.getElementById("selected").value; 
 
    var quantity = document.getElementById("textfield2").value; 
 

 
    var salesT = product * quantity; 
 

 
    var taxRate = document.getElementById("state").value; 
 

 
    var taxTotal = taxRate * salesT; 
 

 
    var shipping = document.getElementById("shipping").value; 
 

 
    var totalBill = parseFloat(taxTotal) + parseFloat(salesT) + parseFloat(shipping); 
 

 
    if (isNaN(inNum)) { 
 
    //alert("result of isNaN"); //The input amount is a non numeric string. It is or contains letters and/or spaces 
 
    document.getElementById("total").value = "Invalid amount" 
 
    } else { 
 
    inNum = parseFloat(totalBill.inNum); //Convert input value into a floating point number. toFixed() requires a number value to work with 
 
    document.getElementById("total").value = parseFloat(totalBill) + inNum.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
 
    } 
 

 
    //document.getElementById("total").value = "$" + totalBill; 
 
}
<h1 align=center>Calculate Your Order</h1> 
 
<form name="frmProd" method="post" action=""> 
 
    <table border="1"> 
 
    <tr> 
 
     <td width="53%">Select Product:</td> 
 
     <td width="47%"> 
 
     <select size="1" name="product" id="selected"> 
 
      <option value="0" selected>Please Select a Product</option> 
 
      <option value="1.99">Book</option> 
 
      <option value=".99">Pen</option> 
 
      <option value=".25">Pencil</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Quantity:</td> 
 
     <td> 
 
     <input name="textfield2" type="text" id="textfield2" size="5"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Shipping State (Tax Purposes):</td> 
 
     <td> 
 
     <select size="1" name=state id="state"> 
 
      <option selected value="0.06">Iowa</option> 
 
      <option value="0.085">Illinois</option> 
 
      <option value="0.7">Indiana</option> 
 
      <option value="0.0">Other</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Delivery Method:</td> 
 
     <td> 
 
     <select size="1" name="shipping" id="shipping"> 
 
      <option value="9.50" selected>Standard UPS - 3 Days</option> 
 
      <option value="6.50">US Mail - 5 Days</option> 
 
      <option value="19.95">Overnight - 1 Day</option> 
 
      <option value="0.0">Pickup</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Your Total is:</td> 
 
     <td> 
 
     <input name="total" value=$ 0.00 id="total"> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
    <p> 
 
    <input type="button" value="Calculate My Order Now!" name="submit" onClick="getShipping(inNum)"> 
 
    <input type="reset" name="Reset" id="button" value="Reset Form"> 
 
    </p> 
 
</form>

+1

請仔細閱讀[問],以及如何創建[mcve],以可複製的方式顯示您的錯誤。此外,您可能希望使用Stack Snippets功能(在頁面中帶有<>的按鈕),這會創建一個在Stack Overflow中運行的代碼片段。 –

+0

您正在調用一個函數並傳遞一個未定義的參數 - 您在哪裏定義了inNum? – Haloor

回答

1
inNum = parseFloat(totalBill.inNum); 

totalBill是在這一點上一個數字,所以它不必inNum的參考。

我不知道你試圖用inNum做,但要完全擺脫它可能是你正在尋找的解決方案:

function getShipping() { 
 
    var product = document.getElementById("selected").value; 
 
    var quantity = document.getElementById("textfield2").value; 
 

 
    var salesT = product * quantity; 
 

 
    var taxRate = document.getElementById("state").value; 
 

 
    var taxTotal = taxRate * salesT; 
 

 
    var shipping = document.getElementById("shipping").value; 
 

 
    var totalBill = parseFloat(taxTotal) + parseFloat(salesT) + parseFloat(shipping); 
 

 
    if (isNaN(totalBill)) { 
 
    //alert("result of isNaN"); //The input amount is a non numeric string. It is or contains letters and/or spaces 
 
    document.getElementById("total").value = "Invalid amount" 
 
    } else { 
 
    document.getElementById("total").value = parseFloat(totalBill).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
 
    } 
 

 
    //document.getElementById("total").value = "$" + totalBill; 
 
}
<h1 align=center>Calculate Your Order</h1> 
 
<form name="frmProd" method="post" action=""> 
 
    <table border="1"> 
 
    <tr> 
 
     <td width="53%">Select Product:</td> 
 
     <td width="47%"> 
 
     <select size="1" name="product" id="selected"> 
 
      <option value="0" selected>Please Select a Product</option> 
 
      <option value="1.99">Book</option> 
 
      <option value=".99">Pen</option> 
 
      <option value=".25">Pencil</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Quantity:</td> 
 
     <td> 
 
     <input name="textfield2" type="text" id="textfield2" size="5"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Shipping State (Tax Purposes):</td> 
 
     <td> 
 
     <select size="1" name=state id="state"> 
 
      <option selected value="0.06">Iowa</option> 
 
      <option value="0.085">Illinois</option> 
 
      <option value="0.7">Indiana</option> 
 
      <option value="0.0">Other</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Delivery Method:</td> 
 
     <td> 
 
     <select size="1" name="shipping" id="shipping"> 
 
      <option value="9.50" selected>Standard UPS - 3 Days</option> 
 
      <option value="6.50">US Mail - 5 Days</option> 
 
      <option value="19.95">Overnight - 1 Day</option> 
 
      <option value="0.0">Pickup</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Your Total is:</td> 
 
     <td> 
 
     <input name="total" value=$ 0.00 id="total"> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
    <p> 
 
    <input type="button" value="Calculate My Order Now!" name="submit" onClick="getShipping()"> 
 
    <input type="reset" name="Reset" id="button" value="Reset Form"> 
 
    </p> 
 
</form>

+0

工作就像一個魅力!謝謝! –