【问题标题】:Can any one help me with javascript code [closed]任何人都可以帮助我使用javascript代码[关闭]
【发布时间】:2015-11-21 21:59:03
【问题描述】:

我有一张包含商品、价格和数量的表格。我想通过用户获取数量,然后用户将单击总成本,总价格将出现在它旁边的框中。我需要有关此 javascript 代码的帮助。我很困惑如何为这些单元格提供类或 id 并为其编写 javascript。

  	
	
<table width="300" border="2" cellspacing="2" cellpadding="3" ><tbody> 
  <tr><th scope="col">Menu </th> <th scope="col">Price</th> <th scope="col">Quantity</th> </tr>
  <tr> <td>Shirt</td> <td>$ 12.99</td> <td><input type="text"></td> </tr>
  <tr> <td>Jeans</td> <td>$ 10.99</td> <td><input type="text"></td> </tr>
  <tr> <td>Jacket</td> <td>$ 20.25</td> <td><input type="text"></td> </tr> 
</tbody></table> 
<div> 
  <input type="button" value="Total Cost"> <input type="text"> 
</div> 

【问题讨论】:

  • 欢迎来到 Stackoverflow!请阅读how to ask,提供有用的问题标题和描述您的问题的基本源代码。问“请写我的代码”并不受欢迎。
  • @Sean 我有 HTML 代码。我正在学习 javascript,但我很困惑如何在 javascript 中使用 id 和 class。

标签: javascript php html css


【解决方案1】:

我同意 Leon 的观点,您应该阅读如何编写问题的指南,但这里有一些内容可以帮助您入门:

<!DOCTYPE html>
<html>
<body>

Price: <input type="text" id="price" value="">
Quantity: <input type="text" id="quantity" value="">

<button onclick="calcPrice()">Calculate</button>

Total cost: <input type="text" id="cost" value="">


<script>
function calcPrice() {
    var price = document.getElementById("price").value;
    var quantity = document.getElementById("quantity").value;

    var cost = price * quantity;
    document.getElementById("cost").value = cost;
}
</script>

</body>
</html>

【讨论】:

  • 通过发布不应回答的问题的答案,您鼓励用户继续走这条路。
猜你喜欢
  • 2014-01-25
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 2015-03-15
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多