简体   繁体   中英

How do I pass a argument in this function

I am working On a shopping cart function, but I want to send product id's to This javascript part.

    $(".qtybutton").on("click", function(){
        var $button = $(this);
        var oldValue = $button.parent().find("input").val();
        var id = $button.parent().find("key").val();
        if ($button.text() === "+") {

            var newVal = parseFloat(oldValue) + 1;
            $.post("cart_session.php", {action:'call_this', newVal: newVal },
            function(data) {
                $('#results').html(data);
            });
        } 

This is the html part.

<div class="dec qtybutton">-</div>
<form method="POST">
<input class="cart-plus-minus-box" type="integer" value="2" disabled>
</form>
<div class="inc qtybutton">+</div>

I want to will pass argument with php ex:- function(<?= $key?>) , I want to know how do I do this?

I would suggest putting the product id in the form field as a data attribute. That way you can always associate the product id with the quantity.

 $(".qtybutton").on("click", function() { let $button = $(this), $inc = $button.closest('.q-cont').find("input") let newVal = Math.max(0,+$inc.val() + +$button.data('inc')) $inc.val(newVal); let id = $inc.data('productid'); console.log('newVal:', $inc.val(), 'id:', id) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='q-cont'> <div class="dec qtybutton" data-inc="-1">-</div> <form method="POST"> <input class="cart-plus-minus-box" data-productid="5" type="integer" value="2" disabled> </form> <div class="inc qtybutton" data-inc="1">+</div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM