我需要用最少的脚本或Jquery代码对html页面中的值进行求和
I need to sum the values in a html page with as minimal scripting or Jquery code
我的html代码:
<tr>
<td class="cart_product">
<a href="">
<img src="images/cart/two.png" alt="">
</a>
</td>
<td class="cart_description">
<h4>
<a href="">Colorblock Scuba</a>
</h4>
<p>Web ID: 1089772</p>
</td>
<td class="cart_price">
<p>$59</p>
</td>
<td class="cart_quantity">
<div class="cart_quantity_button">
<a class="cart_quantity_up" href=""> + </a>
<input class="cart_quantity_input" type="text" name="quantity" value="1" autocomplete="off" size="2">
<a class="cart_quantity_down" href=""> - </a>
</div>
</td>
<td class="cart_total">
<p class="cart_total_price" name="price" id="price" class="price">$59</p>
</td>
<td class="cart_delete">
<a class="cart_quantity_delete" href="">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
这里我有一个para标签,显示产品的价格。我将动态填充它,在实际场景中会有很多行。我需要的是para标签中的值:
<td class="cart_total">
<p class="cart_total_price" name="price" id="price" class="price">$59</p>
</td>
需要进行总结。
您可以像这样简单地使用jQuery。
$(document).ready(function() {
var total = 0;
$(".cart_price p").each(function(index){
var price = $(this).text();
price = price.replace("$","");
total += parseFloat(price);
});
console.log(total);//total price result
$("#price").text("$"+total);
});
$(".cart_quantity_input").on("change",function () {
price = 59; // set your price here
quantity = parseInt(document.getElementsByClassName("cart_quantity_input")[0].value); // the number of products
totalPrice = document.getElementById("price"); // tag to show total price
totalPrice.innerHTML = price*quantity+"$"; // calculating and writing
});
var items = document.querySelectorAll("table tr");
for (var i = 0; i < items.length; i++) {
(function(item){
var price = parseFloat(item.querySelectorAll(".cart_price")[0].innerHTML);
var quantity_ip = item.querySelectorAll(".cart_quantity_input")[0];
quantity_ip.onchange = function() {
item.querySelectorAll(".cart_total_price")[0].innerHTML
= "$" + price * quantity_ip.value;
}
})(items[i]);
}
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何设置html元素填充的动画
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何编写HTML输入的JS内联
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量
- 如何使Javascript动态html表及其上的事件
- 在chrome.tabs.onCreated之后加载HTML页面
- 在html中对所选值求和
- 如何自动求和HTML表中共享相同名称的输入值框
- 变量中的 HTML 表:求和行
- 求和所有html字段的货币值
- 如何在后台进行ajax请求和html元素更新
- 如何自动对HTML表中每行上的输入值框求和
- 如何在jsp中使用javascript对数据库生成的html表的每一列中的值求和
- 如何使用javascript在JSP中对数据库生成的HTML表的列进行求和
- HTML表-遍历行并求和字段
- 我需要用最少的脚本或Jquery代码对html页面中的值进行求和