javascript 帮助使用 html 和计算器

javascript help using html and calculator

谁能帮我算出给定以下价格的股票总价??使用 onclick,一旦有人输入他们想要购买的股票数量,我如何才能获得所有 3 只股票的总价?

    <tr>
        <td><b> SHARE PRICE</b></td>
        <td>.93</td>
        <td>.87</td>
        <td>.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 ,000</li>
    <li>0.08% of the trade value if the total trade value is greater than or equal to ,000</li>
    <li>The minimum commission is </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

我建议将价格信息作为数值存储在某处,例如隐藏输入字段或 table 单元格上的 data- 属性,并在您可以关联的那些元素上创建 ID股票购买的投入。然后只需做一些简单的数学运算即可。这是一个使用隐藏输入的示例:

<table>
    <tr>
        <td><b> SHARE PRICE</b></td>
        <td>.93</td>
        <td>.87</td>
        <td>.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 ,000</li>
    <li>0.08% of the trade value if the total trade value is greater than or equal to ,000</li>
    <li>The minimum commission is </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>