gpt4 book ai didi

javascript - 选中复选框添加到购物车

转载 作者:行者123 更新时间:2023-12-03 12:03:56 25 4
gpt4 key购买 nike

所以我的购物车遇到了一些问题。一些 jQuery 问题,我想知道是否有人能给我指出正确的方向。

事情是,我试图让购物车更加动态,如果您选中复选框,该商品就会添加到购物车中,并且也会显示该商品的总价和折扣。

只是在复选框部分遇到问题。

$(document).ready(function($){
$('#cart_listing .quantity').change(function (event) {
$quan = $(this);
console.log($quan.parent().next()[0]);
$quan.parent().next().find('.price').text(function () {
return $quan.val() * parseInt($(this).attr('data-val'), 10) + ' €';
});
var total = 0;
$('#cart_listing .price').each(function(k, v){
total += parseFloat($(v).text(), 10);
});

$('#cart_listing .faster').text(function () {
faster = parseInt($(this).attr('data-val'), 10);
return $(this).attr('data-val' + ' €');
});

var discount_pct = parseInt($("#cart_listing .discount").data("val"), 10);
var discount = -(total * discount_pct/100);

$('#cart_listing .discounted').text('-' + -discount + ' €');
$('#cart_listing #total').text(total + discount + faster + ' €')
});
});
<小时/>

jsFiddle:http://jsfiddle.net/ooh43u6t/

最佳答案

我没有正确理解你的问题,但我认为你想在使用 jQuery 检查时添加复选框的值。

$('input[type=checkbox]').click(function(){
if($(this).is(':checked')){ //use this to see if the checkbox is checked or not
//do something...
}
});

不知道你是否想要这样的东西。如果没有,您能澄清更多吗?谢谢!

编辑:

所以,你基本上使用相同的东西,也许像这样,

if($(this).is(':checked')){
console.log($(this).parent('td').siblings('td').html()); // check this, you'll have the item name. (the first td)
console.log($(this).attr('data-val')); // this will have the value on the "data-val" attribute on your checkbox
}

现在您有了产品名称(第一个日志)和价格(第二个日志)。现在请记住,只有在格式一致的情况下,这才有效,并且您可能希望为所有产品复选框指定一个唯一的 input[type=checkbox],而不是执行通用的 input[type=checkbox]类,因此它不会干扰您可能拥有的其他复选框。希望这可以帮助。如果没有,我很乐意回答更多问题。谢谢!

关于javascript - 选中复选框添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271524/

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