如何在JavaScript函数中设置参数?

时间:2015-02-18 09:14:09

标签: javascript php jquery

我无法在JavaScript函数中设置参数,下面是我的代码:

var product=<?php echo json_encode($product); ?>;
            var uniqueid=<?php echo json_encode($shop_uniqueid); ?>;
            var shop_photo=<?php echo json_encode($photo); ?>;
            var price=<?php echo json_encode($price); ?>;
            var author=<?php echo json_encode($author); ?>;

            //onmouseover='this.width='someWidth'; this.height='someHeight'" onmouseout="this.width='originalWidth'; this.height='originalHeight''

            $('#'+<?php echo $count; ?>).html("<div class='div1'><img class='shopperspic' src='b2b/Product Images/"+uniqueid+"/"+shop_photo+"' alt='' /></div><div class='div2'><span style='font-weight:bold;'>"+product+"</span></div><div class='div3'>By: <span style='text-decoration:underline'>"+author+"</span></div><div class='in_div'><div class='div_price'><img src='header/images/rupee.png' />"+price+"</div><div>|QTY<select><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select>&nbsp;<input type='button' id='add_"+div.id+"' value='Add to cart' onClick='add_to_cart()'/></div></div>");

我想在var product

中传递参数onClick='add_to_cart()'

3 个答案:

答案 0 :(得分:0)

你可以这样做:

<input type='button' id='add_"+div.id+"' value='Add to cart' onClick='add_to_cart(" + product + ")'/>

答案 1 :(得分:0)

当你将它变成javascript变量时,php变量应该在single quotes中,所以你应该这样写。

var product = '<?php echo json_encode($product); ?>';

答案 2 :(得分:-1)

如果将其设置为var,则可以使用:

onClick='add_to_cart(product)'