附加购物车,我如何折扣某些项目
Append cart, how do I discount certain items?
我有一个复选框购物车,我想知道的是,是否有可能使用append()
功能折扣某些项目?例如,我有一个选项,可以将某件物品从基本升级到高级,但我不希望该物品适用折扣。
我有一个额外的问题,在附录中每个项目都有一个数量框,我如何排除某些项目没有数量框,而只是一些文本?
这里小提琴
function buildCart(checked) {
// This one just builds the sidebar on demand. Quite useful if cart contents can be
// updated many ways (eg in another tab).
$("#cart_items").empty();
$('#total').html('0 €'); //default price for your cart
var cart_total = 0; //default (because your cart shows 10)
//For each checked item, add a sidebar element
for (var i = 0; i < checked.length; i++) {
//build cart item as appropriate.
var newItem = $("<div>");
var checkbox = $("input[name='" + checked[i] + "']");
$test1 = checkbox.parent('td').siblings('td').html(); // check this, you'll have the item name. (the first td)
$test2 = checkbox.attr('data-val'); // this will have the value on the "data-val" attribute on your checkbox
cart_total += parseInt($test2); //add the data-val to the cart_total
newItem.append('<table cellspacing="2" cellpadding="0" border="0" width="320"><tr> <td width="170" valign="top" align="left"> <div class="product_name">' + $test1 + '</div> </div> </td> <td width="30" valign="top" align="center"> <input type="text" class="quantity" value="1" maxlength="2" /> </td> <td width="70" valign="top" align="center"> <div class="product_total_cost price" data-val="' + $test2 + '">' + $test2 + ' €</div> </td> <td width="9" valign="top" align="right"> <div class="remove_product">X</div> </td> </tr> </table>');
/*************************/
var discount_pct = parseInt($(".discount").data("val"), 25);
var discount = -(cart_total * discount_pct / 100);
$('.discounted').text('- ' + -discount + ' €');
$('#total').html(cart_total + discount + " €"); //show this value as the total
$("#cart_items").append(newItem);
}
}
HTML
<table cellspacing="2" cellpadding="0" border="0" width="307">
<tr>
<td width="190" valign="top" align="left">
<div class="discount" data-val="10">Discount</div>
</td>
<td width="70" valign="top" align="center">
<div class="discounted" data-val="25">- 0 €</div>
</td>
</tr>
</table>
这是一个数据结构问题。
首先,你需要区分有资格享受折扣的产品和没有资格享受折扣的产品。你还必须区分哪些产品可以编辑数量,哪些不能。
当前,您的所有输入看起来像:
<input type="checkbox" name="gold_package" value="Gold package" data-val="40" />
如果产品类型为"折扣"或"升级",我将首先添加一个data-product-type
属性来设置。
在您的脚本中,您必须将类型discount
的值压入与类型upgrade
不同的数组中。您还可以设置项目类型upgrade
不具有数量框。
您的购物车总数将是(discount[]
*%) + (upgrade[]
)。
相关文章:
- 正在将数据主题添加到所有项目
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何检查管道中未定义的项目
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 学生搜索项目jquery/javascript
- 如何获取不属于我项目的上一页的URL
- Dojo:访问dijit.form.Select中单击的项目
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 我需要为我的朋友在这个项目上提供帮助
- 如何从 HTML 查询中删除项目
- 在Meteor项目中安装并包含npm模块后出错
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 将单击事件添加到附加的项目中
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 在javascript中搜索项目列表的性能
- 如何将本地依赖项添加到npm项目中
- angularjs移除焦点上的活动类并添加到下一个项目
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 附加购物车,我如何折扣某些项目
- 贝宝添加到购物车多个项目的形式与折扣