javascript帮助使用html和计算器 [英] javascript help using html and calculator

查看:108
本文介绍了javascript帮助使用html和计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我获得低于价格的股票总价吗?使用onclick,一旦有人投入了他们想购买的股票数量,我该如何着手获得所选3种股票的总价?

Can someone help me to get the total price of the stock given prices below?? Using the onclick, once someone puts in the number of stocks they want to buy, how do I go about getting the total price of all 3 stocks choosen??

    <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


推荐答案

我建议将价格信息作为数值存储在某处,如隐藏的输入字段或表格单元格中的 data - 属性,并在这些元素上创建ID你可以关联股票购买的输入。那么这只是一个简单的数学问题。以下是一个使用隐藏输入的示例:


I would suggest storing the price information as a numeric value somewhere, like in hidden input fields or data- attributes on the table cells, and create IDs on those elements that you can associate with the inputs for stock purchases. Then it's just a matter of doing some simple math. Here's an example using hidden inputs:

<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> 块。

Here's the code for putting the total into a textbox. This would go at the end of your form and replace the <script> block from the first example.

<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>

这篇关于javascript帮助使用html和计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆