【问题标题】:javascript help using html and calculatorjavascript 帮助使用 html 和计算器
【发布时间】:2015-05-16 06:29:39
【问题描述】:

有人可以帮我在给定以下价格的情况下获得股票的总价格吗?使用 onclick,一旦有人输入了他们想要购买的股票数量,我该如何获得所有 3 只股票的总价格??

    <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

【问题讨论】:

  • 到目前为止您尝试过什么?什么不起作用?

标签: javascript html onclick calculator


【解决方案1】:

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

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

这是将总数放入文本框中的代码。这将在您的表单末尾并替换第一个示例中的 &lt;script&gt; 块。

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

【讨论】:

  • 哦,非常感谢。我认为这行得通。但是答案是在提示中给出的,如何转换成文本框呢?我是否遵循我对前一个所做的操作?
  • @Mary 你可以使用 .value dom 属性将数据插入到 textarea 中
  • 很抱歉打扰。但它不起作用。也许我做错了。总数仍会作为警报弹出。
  • 我应该更改 alert("Your total is: $" + total) 中的 alert 部分;
  • @Mary 是的,您需要用任何代码替换 alert(...) 行,以便将您的总值放到页面上(无论是使用 .值,或使用 .innerText 或 .innerHTML 的另一个 HTML 元素)。
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多