2013-04-09 47 views
0

我想將總量乘以價格和「顯示」。總計需要命名爲「金額」,以便將總成本轉移到網關。正如你所看到的,我試圖創造一個能夠使用數量的解決方案。需要計算2個輸入字段並在第3個顯示

這裏所有的信息都是爲​​了測試目的,所以不是個人的。

<script type="text/javascript"> 
function totalprice() { 
    var qty = document.getElementById("quantity").value; 
    var price = 219999; 
    var total = (qty * price); 
    document.getElementById("tot").value = total; 
} 
</script> 

<form action="https://gateway.charityclear.com/hosted/" method="post"> 
<input type="hidden" name="merchantID" value="0000992"> 
<input type="hidden" name="countryCode" value="826"> 
<input type="hidden" name="currencyCode" value="826"> 
<table> 
<tr> 
<td>Full Name </td><td><input type="text" name="customerName" value=></td></tr> 
<tr> 
<td>Full Shipping Address <br>(including Country)<br>(must be same as billing  address)</td><td><textarea rows="4" name="customerAddress" value=></textarea></td></tr> 
<tr> 
<td>Post Code </td><td><input type="text" name="customerPostCode" value=></td>   </tr> 
<tr> 
<td>Email Address </td><td><input type="text" name="customerEmail" value=></td> </tr> 
<tr> 
<td>Phone Number <br>(required for delivery)</td><td><input type="text" name="customerPhone" value=></td></tr> 

<input type="hidden" name="redirectURL" value="http://www.UKRobstep.com/order- successful.html"> 
<tr><td></td> 
</tr> 
<tr><td><input type="hidden" name="orderRef" value="Colour">Colour</td> 
<td> 
<select name="orderRef"> 
<option value="Select a Colour">Select a Colour 
<option value=" Robin M1 in Black">Black 
<option value=" Robin M1 in White "> White 
<option value=" Robin M1 in Red"> Red 
<option value=" Robin M1 in Yellow ">Yellow 
<option value=" Robin M1 in Silver/Grey "> Silver/Grey 
</select></td> 
</tr> 
<tr><td> 

Quantity</td><td><input type="text" name="quantity" id="quantity" class="field" value="1" /></td></tr> 
<tr><td>Price Per Unit</td><td><input type="text" name="price" id="price" class="field" value="£2199.99" readonly="readonly"/> 
<input type="hidden" name="amount" id="tot" class="field" value=""/> 
</td></tr> 

</table> 
<INPUT TYPE="image" SRC="http://www.weebly.com/uploads/9/8/2/8/9828047/5792561_orig.png" BORDER="0" ALT="Pay Now" > 
</form> 

我希望有人能幫忙,提前致謝。

+0

如果需要命名爲「金額」,爲什麼不把它命名爲「金額?」。 – 2013-04-09 04:09:00

回答

0

使用parseInt();

function totalprice() 
{ 
    var qty = document.getElementById("quantity").value; 
    var price = 219999; 
    var total = (parseInt(qty) * price); 
    -------------^^^^^^^^^-------------- 
    document.getElementById("tot").value = total; 
} 
+0

不,完全沒有。 '*'操作符會將兩個操作數強制轉換爲'Number'並執行計算。結果將是一個數字或'NaN' – Ian 2013-04-09 04:27:10

+0

[演示](http://jsfiddle.net/kZ2Gb/7/)在這個演示中,我用jQuery調用function..this只是爲了告訴你它的工作。 – 2013-04-09 04:35:10

+0

如果你**不包含'parseInt'轉換,它**的工作方式**完全相同。 – Ian 2013-04-09 04:36:28

0

你只是想辦法用戶輸入數量後得到的值?我不確定你的意思是「繞過」。按下Enter鍵後,返回值的方法如下。

<script> 
function totalprice() { 
    var KeyID = event.keyCode; 
    if(KeyID == 13){ 
     var qty = document.getElementById("quantity").value; 
     var price = 219999; 
     var total = (qty * price); 
     document.getElementById("tot").value = total; 
      alert(total); 
    } 
    else{ 
    } 
} 

</script> 


<form action="https://gateway.charityclear.com/hosted/" method="post"> 
<input type="hidden" name="merchantID" value="0000992"> 

<input type="hidden" name="countryCode" value="826"> 
<input type="hidden" name="currencyCode" value="826"> 
<table> 
<tr> 
<td>Full Name </td><td><input type="text" name="customerName" value=></td></tr> 
<tr> 
<td>Full Shipping Address <br>(including Country)<br>(must be same as billing  
address)</td><td><textarea rows="4" name="customerAddress" value=></textarea></td> 
</tr> 
<tr> 
<td>Post Code </td><td><input type="text" name="customerPostCode" value=></td>   
</tr> 
<tr> 
<td>Email Address </td><td><input type="text" name="customerEmail" value=></td> </tr> 
<tr> 
<td>Phone Number <br>(required for delivery)</td><td><input type="text" 
name="customerPhone" value=></td></tr> 

<input type="hidden" name="redirectURL" value="http://www.UKRobstep.com/order- 
successful.html"> 
<tr><td></td> 
</tr> 
<tr><td><input type="hidden" name="orderRef" value="Colour">Colour</td> 
<td> 
<select name="orderRef"> 
<option value="Select a Colour">Select a Colour 
<option value=" Robin M1 in Black">Black 
<option value=" Robin M1 in White "> White 
<option value=" Robin M1 in Red"> Red 
<option value=" Robin M1 in Yellow ">Yellow 
<option value=" Robin M1 in Silver/Grey "> Silver/Grey 
</select></td> 
</tr> 
<tr><td> 

Quantity</td><td><input type="text" name="quantity" id="quantity" class="field"  
value="1" onkeydown="return totalprice(this, event);" /></td></tr> 
<tr><td>Price Per Unit</td><td><input type="text" name="price" id="price" 
class="field" value="£2199.99" readonly="readonly"/> 
<input type="hidden" name="amount" id="tot" class="field" value=""/> 
</td></tr> 

</table><INPUT TYPE="image"  
SRC="http://www.weebly.com/uploads/9/8/2/8/9828047/5792561_orig.png" BORDER="0" 
ALT="Pay Now" > </form> 
相關問題