javascript - 在jQuery中将价格的所有值与数量相乘并相加
问题描述
我做了一个小提琴来准确地展示我需要的东西。只需显示所有点击项目的总价及其数量。所有项目及其各自的属性都是动态生成的,因此每个项目都有其唯一的 jquery 脚本。虽然它们都是一样的,但我正在发布其中一个。他们工作正常。每个脚本的末尾只缺少一件事来显示总价并将它们全部加到一个总价 ID 中。
function incrementValue2047() {
var value = parseInt(document.getElementById("qty2047").value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById("qty2047").value = value;
$("#qty-counter2047").addClass("active");
$("#qty-counter-buttons2047").addClass("active");
}
function clickup2047() {
var el = document.getElementById("qty2047");
el.value = Number(el.value) + 1;
if (el.value == 10) return false;
}
function clickdown2047() {
var el = document.getElementById("qty2047");
if (el.value == 0) return false;
el.value = Number(el.value) - 1;
if (document.getElementById("qty2047").value == "0") {
$("#qty-counter-buttons2047").removeClass("active");
$("#qty-counter2047").removeClass("active");
$("#menu_attr_checkbox_12_2047").prop("checked", false);
}
}
解决方案
要实现您需要的内容,您需要遍历每个“数量”输入,将其乘以相关商品的价格并将其添加到运行总计中。
但是,由于您的代码有太多不必要的重复,我强烈建议您先将其干燥。您可以通过删除所有不必要id
的属性并根据元素的行为按类对 HTML 内容进行分组来实现此目的。从那里您可以将相同的单击事件处理程序应用于数量递增/递减控件,以便它适用于列表中的所有项目。
尝试这个:
let updateTotal = () => {
let total = 0;
$('.qty-count').each((i, el) => {
let $container = $(el).closest('.qty-check');
total += el.value * $container.find('.menu_attribute_sum').data('price_plus');
});
$('.total-value').text(total.toFixed(2) + '€');
}
$('.amend-qty').on('click', e => {
e.preventDefault();
let $el = $(e.target);
let $container = $el.closest('.qty-check');
$container.find('.qty-count').val((i, v) => Math.max(0, parseInt(v, 10) + $el.data('dir')));
updateTotal();
});
.attr-group {
width: 400px;
margin: 0 auto;
display: table
}
.checkbox {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
position: relative;
}
.checkbox-input {
display: none;
}
.checkbox label {
width: 100%;
margin: 0;
padding-left: 30px !important;
display: flex;
font-weight: 300;
height: auto;
min-height: 30px;
justify-content: space-between;
align-items: center;
}
.checkbox-label:before {
content: "";
display: flex;
justify-content: center;
align-items: center;
width: 25px;
height: 25px;
margin: 0px;
border-radius: 0.5rem;
text-align: center;
background: #f2f4f5;
position: absolute;
margin-left: -30px;
border: 1px solid #141414;
}
.checkbox-input:checked+.checkbox-label:before {
content: "\2713";
font-size: 18px;
color: #fff;
background: #515D67;
border-color: #515D67;
}
.qty-count {
min-width: 25px;
max-width: 25px;
min-height: 25px;
max-height: 25px;
border-radius: 1rem;
line-height: 15px;
text-align: center;
font-size: 1.2rem;
padding: 0 !important;
margin-right: 1rem;
}
.qty-check label {
padding-left: 0 !important;
justify-content: flex-start !important;
align-items: flex-start;
}
.qty-check .name {
padding-right: 90px;
}
.qty-check label:before {
display: none !important;
}
.qty-check .checkbox-input:checked+.checkbox-label .qty-count {
background: #515D67 !important;
color: #fff !important;
}
.qty-counter {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 3;
display: none;
}
.qty-counter.active {
display: block;
}
.counter-buttons {
position: absolute;
right: 50px;
min-width: 50px;
z-index: 4;
display: flex;
align-items: center;
border-radius: 1rem;
background: #fff;
border: 1px solid #dee2e5;
display: none;
overflow: hidden;
}
.counter-buttons.active {
display: flex;
}
.counter-buttons a {
min-width: 30px;
height: 20px;
font-size: 1.8rem;
line-height: 20px;
text-align: center;
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-user-select: none;
/* Safari */
-khtml-user-select: none;
/* Konqueror HTML */
-moz-user-select: none;
/* Old versions of Firefox */
-ms-user-select: none;
/* Internet Explorer/Edge */
user-select: none;
/* Non-prefixed version, currently
supported by Chrome, Edge, Opera and Firefox */
}
.counter-buttons a:hover {
background: #f2f4f5;
}
.price {
margin-left: auto
}
.total-value {
margin-left: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="attr-group">
<!-- Item 1 -->
<div class="checkbox qty-check">
<span class="qty-counter active" data-amount="1.00"></span>
<span class="counter-buttons active">
<a class="amend-qty" data-dir="1" href="#">+</a>
<a class="amend-qty" data-dir="-1" href="#">-</a>
</span>
<input type="checkbox" name="menu_attribute[12][]" value="2047" id="menu_attr_checkbox_12_2047" class="checkbox-input menu_attribute_sum" data-price_plus="1.0" form="addedBasketFormOptions" />
<label for="menu_attr_checkbox_12_2047" class="checkbox-label amend-qty" data-dir="1">
<input type="text" value="0" name="menu_attribute_qty[2047]" class="qty-count" />
<span class="name mr-auto">CocaCola</span>
<span class="price ml-auto">1.00 €</span>
</label>
</div>
<!-- Item 2 -->
<div class="checkbox qty-check">
<span class="qty-counter active" data-amount="1.40"></span>
<span class="counter-buttons active">
<a class="amend-qty" data-dir="1" href="#">+</a>
<a class="amend-qty" data-dir="-1" href="#">-</a>
</span>
<input type="checkbox" name="menu_attribute[12][]" value="2047" id="menu_attr_checkbox_12_2047" class="checkbox-input menu_attribute_sum" data-price_plus="1.4" form="addedBasketFormOptions" />
<label for="menu_attr_checkbox_12_2047" class="checkbox-label amend-qty" data-dir="1">
<input type="text" value="0" name="menu_attribute_qty[2047]" class="qty-count" />
<span class="name mr-auto">Fanta</span>
<span class="price ml-auto">1.40 €</span>
</label>
</div>
<!-- Item 3 -->
<div class="checkbox qty-check">
<span class="qty-counter active" data-amount="1.40"></span>
<span class="counter-buttons active">
<a class="amend-qty" data-dir="1" href="#">+</a>
<a class="amend-qty" data-dir="-1" href="#">-</a>
</span>
<input type="checkbox" name="menu_attribute[12][]" value="2047" id="menu_attr_checkbox_12_2047" class="checkbox-input menu_attribute_sum" data-price_plus="1.4" form="addedBasketFormOptions" />
<label for="menu_attr_checkbox_12_2047" class="checkbox-label amend-qty" data-dir="1">
<input type="text" value="0" name="menu_attribute_qty[2047]" class="qty-count" />
<span class="name mr-auto">Mythos</span>
<span class="price ml-auto">1.40 €</span>
</label>
</div>
<!-- Total -->
<div style="display:flex;width:100%; justify-content:space-between">
<h2>Total</h2>
<h2 class="total-value">0.00€</h2>
</div>
</div>
推荐阅读
- html - 使html中的左卡作为按钮反应以获取模态窗口
- javascript - ExtJS,如何将 ViewModel 绑定到组件配置参数?
- python - Tkinter:使用鼠标绘制矩形,并以该矩形的形状裁剪照片
- mysql - 如何在 sql 中使用 group_concat() 获取不同的值
- php - wp-cli 错误“/usr/bin/env:«mysql»:不存在 el archivo o el directory”
- vba - 使用 vba 语句或代码更新文本文件内容
- c++ - 为什么我输出如下示例时会出现错误?
- javascript - jQuery滚动到另一个页面上的部分
- javascript - 当下一个 js 发生错误时,Typescript 编译器不会停止
- python - MacOS 与 Windows 上的 Tkinter 网格列间距是否存在固有差异?如果是这样,我该如何解决?