如何使用jquery提醒数量增加和数量减少值?

时间:2018-09-06 06:54:57

标签: php jquery

<script>
    $(document).ready(function(){
        $(".qty-increase, .qty-decrease").click(function(event){
            id = this.id;
            quantity = $("#input-quantity1_"+id).val();
            alert(quantity);
        });
    });
</script>
<div class="input-content">
    <div class="box-qty">
        <input type="text" name="quantity" value="1" id="input-quantity1_<?php echo $row_cart['id']; ?>" class="input-text qty">
        <input type="hidden" name="product_id" value="44">
        <div class="qty-arrows" style="margin-top: -40px;">
            <input type="button" value="+" onclick="var qty_el = document.getElementById('input-quantity1');
        var qty = qty_el.value;if (!isNaN(qty))qty_el.value++;return false;" id="<?php echo $row_cart['id']; ?>" class="qty-increase">

            <input type="button" value="-" onclick="var qty_el = document.getElementById('input-quantity1');
        var qty = qty_el.value;if (!isNaN(qty))qty_el.value--;return false;" id="<?php echo $row_cart['id']; ?>" class="qty-decrease">
        </div>
    </div>
</div>

在此代码中,我有数量增加和数量减少类型按钮。当我点击qty-increase时,价值就会增加。减少时类似。现在,当我单击qty-increase时发生的事情是,它仅警报值1,而值保持不变。因此,如果增加或减少,如何获得价值?请帮助我。

1 个答案:

答案 0 :(得分:0)

请检查以下代码

<script>
    $(document).ready(function(){
        $(".qty-increase").click(function(event){
            id = this.id;
            quantity = $("#input-quantity1_"+ id).val();
            quantity =  parseInt(quantity)+1;
            $("#input-quantity1_"+ id).val(quantity);
        });

        $(".qty-decrease").click(function(event){
            id = this.id;
            quantity = $("#input-quantity1_"+ id).val();
            quantity =  parseInt(quantity)-1;
            if(quantity > 0){
                $("#input-quantity1_"+ id).val(quantity);
            }else{
                $("#input-quantity1_"+ id).val(0);
                alert('Item is remove in cart')
            }

        });
    });
</script>

让我知道您是否仍然遇到错误。

更新:-

   <div class="box-qty">
        <input type="text" name="quantity" value="1" id="input-quantity1_<?php echo $row_cart['id']; ?>" class="input-text qty">
        <input type="hidden" name="product_id" value="44">
        <div class="qty-arrows" style="margin-top: -40px;">
            <input type="button" value="+" onclick="cart_value('add',<?php echo $row_cart['id']; ?>);" id="<?php echo $row_cart['id']; ?>" class="qty-increase">

            <input type="button" value="-" onclick="cart_value('less',<?php echo $row_cart['id']; ?>);" id="<?php echo $row_cart['id']; ?>" class="qty-decrease">

            <input type="button" value="add" onclick="add_value_weclome();">
        </div>
    </div>

<script>
    function cart_value(data,id){
        if(data == 'add'){
            quantity = $("#input-quantity1_"+ id).val();
            quantity =  parseInt(quantity)+1;
            $("#input-quantity1_"+ id).val(quantity);
        }else{
            quantity = $("#input-quantity1_"+ id).val();
            quantity =  parseInt(quantity)-1;
            if(quantity > 0){
                $("#input-quantity1_"+ id).val(quantity);
            }else{
                $("#input-quantity1_"+ id).val(0);
                alert('Item is remove in cart')
            }
        }
    }
</script>