0

I am trying to get a variable (set on the front end) to be sent through jQuery onClick.

I'm pretty sure it's possible, I just can't figure it out because I'm not familiar with jQuery.

Here is the HTML part:

<input type="text" name="quantity" size="2" value="1" />
<input type="button" value="Add to cart" id="button-cart" class="button" />

And here is my jQuery part:

$('#button-cart').bind('click', function() {
    addToCart(
        '<?php echo $product_id; ?>',
        '$("#quantity").val();'
    );
});

But it just doesn't work. I know if I do something like this:

$('#button-cart').bind('click', function() {
    addToCart(
        '<?php echo $product_id; ?>',
        '3'
    );
});

That it adds 3 items to the cart but the page needs to be dynamic so the user can enter the quantity needed.

I hope it's clear what I'm trying to do. If anybody could help that would be great. Thank you.

4

3 回答 3

0

try like this :

<script type="text/javascript">
$('#button-cart').bind('click', function() {
    addToCart(
        '<?php echo $product_id; ?>',
        $("input[name=quantity]").val()
    );
});
</script>
于 2013-10-30T18:31:34.703 回答
0

1) $("#quantity").val(); 不应该在里面''

2) 使用 $(document).ready

3)按名称选择,您应该使用 [name=quantity],而不是 #quantity(即用于 ids)

<script type="text/javascript">
$(document).ready(function ()
{
$('#button-cart').click(function() {
    addToCart(
        '<?php echo $product_id; ?>',
        $("input[name=quantity]").val()
    );
});
});
</script>
于 2013-10-30T18:34:24.853 回答
0

尝试这个:

<input type="text" name="quantity" size="2" value="1" />
 <input type="button" onclick="addToCart('<?php  echo $product_id;?>')" value="Add to cart" id="button-cart" class="button" />



<script>
function addToCart(id){
var quantity=$("input[name=quantity]").val();
var productId=id;

//Do your stuff
}


</script>
于 2013-10-30T18:41:28.177 回答