我有2个按钮,一个+和一个-增加项目数量。
我也有该商品的“价格”和一个显示该商品价格的跨度。
我需要的是:当用户单击+或-按钮时,它将增加项目数量和总价格。
到目前为止,我的代码如下所示:
HTML:
<input type="number" size="4" class="input-text qty text" title="Cantidad" value="1" name="quantity" min="1" step="1">
<input type="button" class="plus" value="+">
<input type="button" class="minus" value="-">
<span class="price">90.00 €</span>
JS
function inc() {
var price = "price";
var elems = document.getElementsByTagName('*'), i;
for (i in elems) {
var a = document.getElementsByName("quantity")[0].value;
if((' ' + elems[i].className + ' ').indexOf(' ' + price + ' ') > -1) {
var valOfItem = parseInt(elems[i].innerHTML);
var x = a * valOfItem;
elems[i].innerHTML = x;
}
}
}
我还没有尝试过,但是我想只要将func()添加到这些按钮上的onClick事件中,一切都将正常工作,我希望在文档加载完成后使用jQuery来完成操作,但我没有确定如何。
有任何想法吗?
您可以使用和将click
事件处理程序添加到.plus
并.minus
根据input
值计算总和:.text()
.val()
小提琴。
$(document).ready(function()
{
var basePrice = parseFloat($(".price").text());
$(".plus").click(function()
{
changeValue(1);
});
$(".minus").click(function()
{
changeValue(-1);
});
function changeValue(sign)
{
$("[name='quantity']").val(parseInt($("[name='quantity']").val()) + sign);
var countValue = $("[name='quantity']").val();
var newValue = (basePrice * countValue).toFixed(2);
$(".price").text(newValue);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句