Jquery ajax调用ID值不对

时间:2013-05-06 19:07:03

标签: php jquery magento

以下函数进行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); 
    });
}

2 个答案:

答案 0 :(得分:1)

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

$j(document).on("click","#" + value, function () {
    alert('test');
});

答案 1 :(得分:0)

我没有必要使用ajax函数,因为我没有返回值。我只需要在我的javascript中使用一个常量的php变量。谢谢你的所有建议。我仍然遇到selectAndAddToCart函数的问题,但我会再次发布以分开问题。