gpt4 book ai didi

javascript - 使用 jquery 获取总价

转载 作者:行者123 更新时间:2023-12-02 23:10:15 24 4
gpt4 key购买 nike

JS 新手,真的陷入困境。我有一个加号和一个减号按钮,用于添加产品。但我需要将总价格的结果相应地加起来。并且有些产品只能以2、5或10的倍数购买。

这里有我的 html 和我的 2 个产品。一种只能以 5 的倍数出售,另一种只能以 10 的倍数出售。

<!-- Product #6 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="intem-6.jpg" alt="" />
</div>

<div class="description">
<span>DLT-19 Heavy Blaster Rifle</span>
<span>Brown</span>
</div>

<div class="quantity">
<button class="plus-btn" type="button" name="button">
<img src="plus-btn.png" alt="" />
</button>
<input type="text" name="name" value="1">
<button class="minus-btn" type="button" name="button">
<img src="minus-btn.png" alt="" />
</button>
</div>

<div id="multiple5" class="total-price">$5800</div>
</div>

<!-- Product #7 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="item-7.jpg" alt="" >
</div>

<div class="description">
<span>DL-44 Heavy Blaster Pistol</span>
<span>Brown</span>
</div>

<div class="quantity">
<button class="plus-btn" type="button" name="button">
<img src="plus-btn.png" alt="" />
</button>
<input type="text" name="name" value="1">
<button class="minus-btn" type="button" name="button">
<img src="minus-btn.png" alt="" />
</button>
</div>

<div id="multiple10" class="total-price">$1500</div>
</div>
</div>

这是我的 jQuery,它添加了 1 个按钮的 on.click。

$('.minus-btn').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var $input = $this.closest('div').find('input');
var value = parseInt($input.val());

if (value >= 1) {
value = value - 1;
} else {
value = 0;
}

$input.val(value);

});

$('.plus-btn').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var $input = $this.closest('div').find('input');
var value = parseInt($input.val());

if (value < 100) {
value = value + 1;
} else {
value =100;
}

$input.val(value);
});

我尝试与我的搜索一起单独进行,但无法在我的其他代码上实现这一点。只是希望结果与按钮的 on.click 事件一起增加和减少价格。

<html>
<head>
<title>Hello</title>

<style>
*{
font-size: 25px;
}
</style>

</head>

<body>

<button id="plus"> + </button>
<input id="quantity" type="number" value='1'>
<button id="minus"> - </button>
<div id="total">5</div>


<script>

let plus = document.getElementById('plus');
let quantityEle = document.getElementById('quantity');
let minus = document.getElementById('minus');
let total = document.getElementById('total');

let quantity = quantityEle.value;
let price = 5;

plus.addEventListener('click', function(){
quantity++;
quantityEle.value = quantity;
total.innerHTML = price * quantity;

});

minus.addEventListener('click', function(){
if(quantity > 1)
quantity--;
quantityEle.value = quantity;
total.innerHTML = price * quantity;
});

quantityEle.addEventListener('change', function(){
quantity = quantityEle.value;
total.innerHTML = price * quantity;
});

var total = $this.closest('div').find('div');

</script>


</body>
</html>

最佳答案

给你!

$('.minus-btn').on('click', function(e) {
e.preventDefault();
var $parent = $(this).closest('.item');
var $input = $parent.find('input');
var $total = $parent.find('.total-price span');
var value = parseInt($input.val());

if (value >= 1) {
value = value - 1;
} else {
value = 0;
}

$input.val(value);
$total.html(value);
calcTotal();
});

$('.plus-btn').on('click', function(e) {
e.preventDefault();
var $parent = $(this).closest('.item');
var $input = $parent.find('input');
var $total = $parent.find('.total-price span');
var value = parseInt($input.val());

if (value < 100) {
value = value + 1;
} else {
value = 100;
}

$input.val(value);
$total.html(value);
calcTotal();
});

function calcTotal() {
let total = 0;
$(".total-price span").each(function() {
total += parseInt($(this).html());
});
$('#total').html('$' + total);
}
#total {
color: red;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<!-- Product #6 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="intem-6.jpg" alt="" />
</div>

<div class="description">
<span>DLT-19 Heavy Blaster Rifle</span>
<span>Brown</span>
</div>

<div class="quantity">
<button class="plus-btn" type="button" name="button">
<img src="plus-btn.png" alt="" /> +
</button>
<input type="text" name="name" value="1">
<button class="minus-btn" type="button" name="button">
<img src="minus-btn.png" alt="" /> -
</button>
</div>

<div id="multiple5" class="total-price">$<span>1</span></div>
</div>

<!-- Product #7 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="item-7.jpg" alt="">
</div>

<div class="description">
<span>DL-44 Heavy Blaster Pistol</span>
<span>Brown</span>
</div>

<div class="quantity">
<button class="plus-btn" type="button" name="button">
<img src="plus-btn.png" alt="" /> +
</button>
<input type="text" name="name" value="1">
<button class="minus-btn" type="button" name="button">
<img src="minus-btn.png" alt="" /> -
</button>
</div>

<div id="multiple10" class="total-price">$<span>1</span></div>
</div>

<div>
Total: <span id="total">$2</span>
</div>

关于javascript - 使用 jquery 获取总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57403296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com