dpi96151 2013-09-20 12:39
浏览 23

Ajax / php如何识别单个通用字段?

I'm building a checkout on a marketplace application. I need to be able to increment, decrement and remove items at the checkout but when there is multpile items the AJAX event jQuery only picks up the first item_id when the AJAx event fires no mater what item a user clicks on, how can I fix this?

Here's the PHP/HTML code...

        <?php
            $total      = 0;
            $subtotal   = 0;
            foreach($items as $item):
                $subtotal   += $item['product_price']*$item['product_qty'];
        ?>
        <tbody id="item_<?=$item['cart_item_id']?>">
            <tr>
                <td>
                    <img src="<?=$item['product_image']?>">
                    <?php if($item['brand_name']!='None'): ?>
                    <p style="color: #666;"><?=$item['brand_name']?></p>
                    <?php endif; ?>
                    <p style="font-size: 16px;"><?=$item['product_name']?></p>
                    <a href="#" id="remove-item-button" >Remove this item</a> <!--HERE-->
                </td>
                <td class="align-td-center">
                    <?=formatted_price($item['product_price'])?>
                </td>
                <td class="align-td-center">
                    <a href="#" id="qty-inc-button" >More</a><?=$item['product_qty']?><a                  href="#" id="qty-dec-button">Less</a><!--AND HERE-->
                    <input type="hidden" id="item_id" value="<?=$item['cart_item_id']?>">
                </td>
                <td class="align-td-center">
                    <span id="value-shipping-<?=$item['cart_item_id']?>"><?=formatted_price($item['product_price']*$item['product_qty'])?></span>
                </td>
            </tr>
        </tbody>

And here's my jQ/AJAX...

    $('a#qty-inc-button').click(function(){

        $("#processing").fadeIn("fast");
        $.ajax({
            url: "<?=BASE_URL?>landing/inc_cart_item",
            type: "post",
            dataType: "json",
            data: {inc:1, item_id: $('#item_id').val(),},
            success: function(data){
                $("#processing").fadeOut("fast");
                if(data.error) {
                    alert(data.error);
                } else {
                   // parent.$('#line1').text(data.line1);
                    //parent.$('#line2').text(data.line2);
                    //parent.$('#address-dialog').dialog('close');
                    alert(data.line1);
                }
            }
        });
        return false;           
});

$('a#qty-dec-button').click(function(){

    $("#processing").fadeIn("fast");
    $.ajax({
        url: "<?=BASE_URL?>landing/dec_cart_item",
        type: "post",
        dataType: "json",
        data: {dec:0, item_id: $('#item_id').val(),},
        success: function(data){
            $("#processing").fadeOut("fast");
            if(data.error) {
                alert(data.error);
            } else {
                //parent.$('#line1').text(data.line1);
                //parent.$('#line2').text(data.line2);
                //parent.$('#address-dialog').dialog('close');
                alert(data.line1);
            }
        }
    });
    return false;           
});

$('a#remove-item-button').click(function(){

    $("#processing").fadeIn("fast");
    $.ajax({
        url: "<?=BASE_URL?>landing/rem_cart_item",
        type: "post",
        dataType: "json",
        data: {item_id: $('#item_id').val(),},
        success: function(data){
            $("#processing").fadeOut("fast");
            if(data.error) {
                alert(data.error);
            } else {
                //parent.$('#line1').text(data.line1);
                //parent.$('#line2').text(data.line2);
                //parent.$('#address-dialog').dialog('close');
                alert(data.line1);
            }
        }
    });
    return false;           
});

All help appreciated and thanks for looking.

  • 写回答

3条回答 默认 最新

  • dongya6997 2013-09-20 12:44
    关注

    An id is meant to be an unique identifier on a page. http://css-tricks.com/the-difference-between-id-and-class/

    So, JQuery stops searching as soon as it finds the first occurrence of #qty-inc-button.

    There are many ways to do what you are trying to acheive. You should make qty-inc-button a class name along with any id that appears multiple times on a page. Then you can try

    $('a.qty-inc-button').click(function(){
        $(this).DoSomething();  // where $(this) is the 'a.qty-inc-button' that the user clicked on.
    
        // you can do:
        $(this).children(".aChildClass").hide();
    
        // you can do:
        $(this).find( 'someElement' ).css( 'background-color', 'red' ); //etc, etc
    });
    

    However, in my very humble opinion you should learn more about HTML attributes and JQuery traversal before trying things of this caliber. Otherwise, you'll keep stumbling.

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢