2015-03-13 120 views
-3

有人可以幫助我得到低於股票價格的總價?使用onclick,一旦有人投入了他們想購買的股票的數量,我該如何去獲得所有3種股票的總價格?javascript幫助使用html和計算器

<tr> 
     <td><b> SHARE PRICE</b></td> 
     <td>$43.93</td> 
     <td>$43.87</td> 
     <td>$26.33</td> 
    </tr> 
</table> 
<hr> 
<br> 
<p> 
<h3>Important information that you should know about these stocks: </h3> 
<ul> 
    <li>0.1% of the trade value if the total trade value is less than $10,000</li> 
    <li>0.08% of the trade value if the total trade value is greater than or equal to $10,000</li> 
    <li>The minimum commission is $5</li> 
    <hr> 
    <br> 
</ul> 

<form name="calculator"> 

<p> Enter the number of Oracle Corporation stocks you wish to purchase!: <input type="text" id="input1"></p> <br> 
<p> Enter the number of Microsoft Corporation stocks you wish to purchase!: <input type="text" id="input2"></p> <br> 
<p> Enter the number of Symantec Corporation stocks you wish to purchase!: <input type="text" id="input3"></p> <br 
+3

你到目前爲止試過的是什麼?什麼沒有用? – Teemu 2015-03-13 17:02:29

回答

1

我建議某處存儲的價格信息爲數值,就像隱藏輸入字段或表格單元格data-屬性,並可以與輸入的關聯這些元素創建標識股票購買。那麼這只是一個簡單的數學問題。以下是使用隱藏輸入的示例:

<table> 
    <tr> 
     <td><b> SHARE PRICE</b></td> 
     <td>$43.93</td> 
     <td>$43.87</td> 
     <td>$26.33</td> 
    </tr> 
</table> 
<h3>Important information that you should know about these stocks: </h3> 
<ul> 
    <li>0.1% of the trade value if the total trade value is less than $10,000</li> 
    <li>0.08% of the trade value if the total trade value is greater than or equal to $10,000</li> 
    <li>The minimum commission is $5</li> 
</ul> 

<form name="calculator"> 
    <input type="hidden" id="price1" value="43.93" /> 
    <input type="hidden" id="price2" value="43.87" /> 
    <input type="hidden" id="price3" value="26.33" /> 
    <p> Enter the number of Oracle Corporation stocks you wish to purchase!: <input type="text" id="input1"></p> <br> 
    <p> Enter the number of Microsoft Corporation stocks you wish to purchase!: <input type="text" id="input2"></p> <br> 
    <p> Enter the number of Symantec Corporation stocks you wish to purchase!: <input type="text" id="input3"></p> <br> 
    <input type="button" value="Add!" onclick="javascript:sumUp()" /> 
</form> 

<script type="text/javascript"> 
    function sumUp() { 
     var total = (document.getElementById("price1").value * document.getElementById("input1").value) + (document.getElementById("price2").value * document.getElementById("input2").value) + (document.getElementById("price3").value * document.getElementById("input3").value) 
     alert("Your total is: $" + total); 
    } 
</script> 

下面是將總數放入文本框的代碼。這將在您的表單的結尾,並從第一個示例中替換<script>塊。

<p>Your total is: $<input type="text" id="total" /></p> 

<script type="text/javascript"> 
    function sumUp() { 
     var total = (document.getElementById("price1").value * document.getElementById("input1").value) + (document.getElementById("price2").value * document.getElementById("input2").value) + (document.getElementById("price3").value * document.getElementById("input3").value) 
     document.getElementById("total").value = total; 
    } 
</script> 
+0

非常感謝你。我認爲這是有效的。但答案是在提示中給出,我如何轉換成文本框?我遵循我爲前一個做的事情嗎? – Mary 2015-03-13 17:18:52

+0

@Mary你可以使用.value dom propert將數據插入到textarea – 2015-03-13 17:20:05

+0

我很抱歉打擾。但它不起作用。也許我做錯了。總數仍然作爲警報彈出。 – Mary 2015-03-13 17:28:56