Problem when calling an input I can not find a solution please help! JQUERY

0

Script where I send the information and show it

<script>
  $(document).ready(function(){

    $('.btcar').click(function(e){
        e.preventDefault();
        var car = $(this).attr("id");
        var stock = $('#stock'+car).val();
        var stock1 = parseInt(stock);  

        var prue = $('#prue').attr("value");  
        var prue1 = parseInt(prue);

        var cant = $('#canti'+prue1).val();
        var cant1 = parseInt(cant);

        alert(cant1);

        if(stock1 > 1 && stock1 != cant1 ){
          $.ajax({

              url:'carrito.php',
              method: 'POST',
              data:{car:car, prue:prue},
              type:JSON,
              success:function(deta){
                $('#carrito').html(deta);
              }
          });
        } else{
          alert ("Exedio el limite");
        }
    }); 

    $('#carrito').load("carrito.php");

  });
</script>

Data I want to receive when I send

It turns out that this input to the I click btcar is generating orders. The point is that those orders have a stock, and that stock I think out of the number of times I click on the btcar that will be stored in the input of class canti obviously if the order is different I generated it in another input the point is that I can take the value of the first input and achievement that does not continue adding when this is the maximum stock but the others that are generated automatically depending on the order that I select does not recognize me always takes the value of First.

<input type="hidden" id="prue" value="<?php echo $i ?>">
<input type="text" class="canti" id="canti<?php echo $i ?>" name="cantidad" min="1" maxlength="<?php echo $shopCart[$i]['stock']; ?>" value="<?php echo $shopCart[$i]['cantidad']; ?>" style="text-alaing: center;">
    
asked by Steban De Abreu 17.08.2018 в 09:09
source

1 answer

0

I think your problem is in the validation you are placing it to be different when it should be if the Stock is greater than or equal to Quantity:

<script>
  $(document).ready(function(){
    $('.btcar').click(function(e){
        e.preventDefault();
        var car = $(this).attr("id");
        var stock = $('#stock'+car).val();
        var stock1 = parseInt(stock);
        var prue = $('#prue').attr("value");  
        var prue1 = parseInt(prue);
        var cant = $('#canti'+prue1).val();
        var cant1 = parseInt(cant);
        alert(cant1);
        if(stock1 > 1 && stock1 >= cant1 ){
          $.ajax({
              url:'carrito.php',
              method: 'POST',
              data:{car:car, prue:prue},
              type:JSON,
              success:function(deta){
                $('#carrito').html(deta);
              }
          });
        } else{
          alert ("Exedio el limite");
        }
    }); 
    $('#carrito').load("carrito.php");
  });
</script>
    
answered by 17.08.2018 в 17:03