-1

以下函数进行 ajax 调用,但我遇到了几个问题。首先,我什至需要进行 ajax 调用吗?唯一的 PHP 值是 $product_id。应用程序进行预先 ajax 调用以使用选择颜色后可用的产品填充页面。其次,按钮元素不断分配错误的数字!最后,分配这样的点击功能是否可行?任何建议表示赞赏。

jQuery:

function addToCartBlock(value)
    {
    console.log ('the function value is ' + value); 
    var product_id = <?= $product_id ?>;
        $j.ajax({
            type: "POST", 
            url: "/ajax_calls/addToCartBlock.php",
            data: { 'value': value, 'product_id': product_id} 
            }).done(function(data) {
                $j('.vendorListItem'+value).append('<span class="add-to-cart" id="add-to-cart'+ value+'"></span>');
                $j('.vendorListItem'+value).css({'display':'inline'});
                $j('#add-to-cart' + value).append('<label for="qty">Qty:</label>'); 
                $j('#add-to-cart' + value).append('<input type="text" name="qty" id="qty" maxlength="12" value="1" title="Qty" class="input-text qty" />');
                $j('#add-to-cart' + value).append('<button type="button" title="" class="button btn-cart" id="' + value + '"><span>Add To Cart</span></button>');

                $j($j('button').attr('id',value)).on('click',function()
                {
                    console.log('The onclick value is ' + $j('button').attr('id'));//this logs a random number btwn 1-3, last add to cart button logs once the others multiple times
                    //take value of list item
                    $j('#attribute136 option[value=' + $j('button').attr('id') + ']').attr('selected', 'selected').ready(function () {;//make the applicable selection
                    $j('#attribute136').val($j('button').attr('id'));

                    console.log($j('#attribute136').val()); //this keeps logging '3'
                    //CAITLIN not all buttons have an ID of the correct value though the prior span class seems to... console.log through the program to debug

                    //initiate add to cart function
                    productAddToCartForm.submit(this); 
                    });
                });
            }); 
    }

PHP 脚本 addtocart.php:

<?php

require_once('/var/www/Staging/public_html/app/Mage.php');
umask(0);
Mage::app(); 

//ensure that the value is legitimate
if($_POST && is_numeric($_POST['value'])){
    $value = $_POST['value'];
}

//pass this in your ajax call for the add button
if($_POST && is_numeric($_POST['product_id'])){
    $product_id = $_POST['product_id'];
}

$helper = Mage::helper('core'); //for translation
$block = new Mage_Catalog_Block_Product_View(); // not best practice, but neither are standalones
$product =  Mage::getModel('catalog/product')->load($product_id); // no need to use the _ here, it's not protected/private; additonally Mage::registry won't work because you're technically not on a product detail page

$buttonTitle = ''; //you are using this, but it isn't set

$resultsArray= [];
$resultsArray['Qty'] = $helper->__('Qty:');
$resultsArray['Qtyno'] = $helper->__('Qty');
$resultsArray['DefaultQty'] = $block->getProductDefaultQty($product);
$resultsArray['windowLocation'] = Mage::helper('checkout/cart')->getAddUrl($product);
$resultsArray['value']=$value;
echo json_encode($resultsArray);

?>

编辑!!!!尝试以下函数而不是 AJAX 调用。我在使用此代码时遇到的唯一问题是,如果选择了 ID 为 3 的按钮,它只会添加到购物车中。我需要所有按钮将其适用的 id 值产品添加到购物车。

function addToCartBlock(value)
    {
    console.log ('the addToCartBlock function value is ' + value); 
    //var product_id = <?= $product_id ?>;

                $j('.vendorListItem'+value).append('<span class="add-to-cart" id="add-to-cart'+ value+'"></span>');
                $j('.vendorListItem'+value).css({'display':'inline'});
                $j('#add-to-cart' + value).append('<label for="qty">Qty:</label>'); 
                $j('#add-to-cart' + value).append('<input type="text" name="qty" id="qty" maxlength="12" value="1" title="Qty" class="input-text qty" />');
                $j('#add-to-cart' + value).append('<button type="button" onclick="selectAndAddToCart('+value+')" title="" class="button btn-cart" id="' + value + '"><span>Add To Cart</span></button>');
    }  

function selectAndAddToCart(value)
{
    console.log('The selectAndAddToCart onclick value is ' + value);
    $j('#attribute136 option[value=' + value + ']').attr('selected', 'selected').ready(function () {;//make the applicable selection
        $j('#attribute136').val(value);

        console.log('Selection made.');     
        console.log($j('#attribute136').val()); //this keeps logging '3'

        //initiate add to cart function
        productAddToCartForm.submit(this); 
    });
}
4

2 回答 2

1

以下代码将帮助您解决动态添加按钮的点击事件问题,

$j(document).on("click","#" + value, function () {
    alert('test');
});
于 2013-05-06T19:19:34.057 回答
0

我不必使用 ajax 函数,因为我不必返回值。我只需要在我的 javascript 中使用一个不变的 php 变量。感谢您的所有建议。我仍然对 selectAndAddToCart 功能有疑问,但我会再次发布以分隔问题。

于 2013-05-06T20:14:41.740 回答