使用按钮f增加和减少数量 [英] Increasing and decreasing the quantity using buttons f
本文介绍了使用按钮f增加和减少数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在按钮上添加功能.我试图将其添加到php文件中.
I need to add functions to the buttons.I tried to add that to the php file.
脚本在这里:
$(document).ready(function(){
$document.on('click','#minus_button',function(){
selected=$(this.attr("data-id"));
quantity = parseInt($('#quantity[data-id="'+selected'"]').val());
$('#quantity[data-id="'+selected'"]').val(quantity -1);
});
$document.on('click','#plus_button',function(){
selected=$(this.attr("data-id"));
quantity = parseInt($('#quantity[data-id="'+selected'"]').val());
$('#quantity[data-id="'+selected'"]').val(quantity + 1);
});
});
以下是按钮:
<button type=\"button\" class=\"btn bg-light border rounded-circle\" id=\"minus_button\" data-id=\"$productid\"><i class=\"fas fa-minus\"></i></button>
<input type=\"text\" value=\"1\" class=\"form-control w-25 d-inline\" id=\"quantity\" data-id=\"$productid\">
<button type=\"button\" class=\" btn bg-light border rounded-circle\"id=\"plus_button\" data-id=\"$productid\"><i class=\"fas fa-plus\"></i></button>
我从数据库中获得了product_id.按钮不起作用.如果您有帮助,我将很高兴.
I get the product_id from database.Buttons do not work.I would be glad if you help.
推荐答案
还有该脚本来计算总价.但是以这种方式,我需要在上面添加数量.当我尝试添加时,它不起作用.
And also that script to calculate total price.But in this manner,I need add quantity on it.When I tried to add that it doesn't work.
<?php
$total = 0;
if (isset($_SESSION['cart'])){
$product_id = array_column($_SESSION['cart'], 'product_id');
$result = $db->getData();
while ($row = mysqli_fetch_assoc($result)){
foreach ($product_id as $id){
if ($row['id'] == $id){
cartElement($row['product_image'], $row['product_name'],$row['product_price'], $row['id']);
$total = $total + (int)$row['product_price'];
}
}
}
}else{
echo "<h5>Cart is Empty</h5>";
}
?>
这篇关于使用按钮f增加和减少数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文