dongwen1871 2018-09-06 06:54
浏览 45

如何使用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>

In this code I have quantity increase and quantity decrease type buttons. When I click on qty-increase then value increases. Similarly in case of decrease. Now, what happens when I click on qty-increase is that it alerts value 1 only and value remains the same. So, how can I get value if I increase or decrease? Please help me.

  • 写回答

1条回答 默认 最新

  • dony113407 2018-09-06 07:14
    关注

    Please check with below code

    <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>
    

    let me know if you still get the error.

    Update:-

       <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题