javascript中的产品id

javascript中的产品id,javascript,php,prestashop,Javascript,Php,Prestashop,我在js方面没有太多经验。我想创建一个变量productId,它有一个产品ID,然后这个变量在 $('#quantity_wanted_'+ productId +').change(); 我该怎么做 <script type="text/javascript">// The button to increment the product value //var allowBuyWhenOutOfStock = false; $(document).on('cli

我在js方面没有太多经验。我想创建一个变量productId,它有一个产品ID,然后这个变量在

$('#quantity_wanted_'+ productId +').change();
我该怎么做

<script type="text/javascript">// The button to increment the product value
    //var allowBuyWhenOutOfStock = false;

    $(document).on('click', '.product_quantity_up', function(e){
        var productId = 6;
        e.preventDefault();
        fieldName = $(this).data('field-qty');
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        if (!allowBuyWhenOutOfStock && quantityAvailable > 0)
            quantityAvailableT = quantityAvailable;
        else
            quantityAvailableT = 100000000;
        if (!isNaN(currentVal) && currentVal < quantityAvailableT)
            $('input[name='+fieldName+']').val(currentVal + 1).trigger('keyup');
        else
            $('input[name='+fieldName+']').val(quantityAvailableT);

        $('#quantity_wanted_'+ productId +').change();
    });
    // The button to decrement the product value
    $(document).on('click', '.product_quantity_down', function(e){
        var productId = 7;
        e.preventDefault();
        fieldName = $(this).data('field-qty');
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        if (!isNaN(currentVal) && currentVal > 1)
            $('input[name='+fieldName+']').val(currentVal - 1).trigger('keyup');
        else
            $('input[name='+fieldName+']').val(1);

        $('#quantity_wanted_'+productId+').change();
    });
</script>
//增加产品价值的按钮
//var AllowBuyWhenowtofstock=假;
$(文档)。在('单击','上。产品数量增加',函数(e){
var-productId=6;
e、 预防默认值();
fieldName=$(this).data('field-qty');
var currentVal=parseInt($('input[name='+fieldName+']').val());
如果(!AllowBuyWhenOtofStock&quantityAvailable>0)
QuantityAvailableET=quantityAvailable;
其他的
QuantityAvailableET=100000000;
如果(!isNaN(currentVal)&¤tVal1)
$('input[name='+fieldName+']').val(currentVal-1.trigger('keyup');
其他的
$('input[name='+fieldName+']').val(1);
$(“#所需数量"+productId+).change();
});

在Smarty中,您可以定义javascript变量

如果查看
product.tpl
文件的底部,您将看到这样一行:

{addJsDef customizationId=$id_customization}
{addJsDef customizationFields=$customizationFields}
{addJsDef default_eco_tax=$product->ecotax|floatval}
在加载javascript文件之前,这些变量会放在网站的标题部分。因此,它可以从任何javascript文件访问

然后,您可以将您的
productId
变量添加到
product.tpl
模板的末尾,并添加另一个:

{addJsDef productId=$product->id}
在我的主题中,名为
id\u product
的变量已经存在,它可能也可以在您的主题中使用:

{addJsDef id_product=$product->id|intval}

您需要在TPL中创建/设置
productId

{addJsDef productId=$id_product}

祝你好运。

请进一步解释,我不知道如何从php读取变量并在javascript中使用它。你不应该在
productId
之后使用
+'
。很高兴知道。我在product-list.tpl中有此代码,其中有许多产品。当我创建/设置{addJsDef id_product=$product->id|intval}时,我得到了0。检查链接