1

我有这样的标记。现在在这里您可以看到每个按钮都有相同的类名为按钮。

  <div class="cart">
    <input type="text" name="quantity" size="2" value="12" />
    <input type="hidden" name="product_id" size="2" value="42" />
    &nbsp;<input type="button" value="add to cart" class="button-cart" class="button" />
  </div>

  <div class="cart">
    <input type="text" name="quantity" size="2" value="132" />
    <input type="hidden" name="product_id" size="2" value="42" />
    &nbsp;<input type="button" value="add to cart" class="button-cart" class="button" />
  </div>

  <div class="cart">
    <input type="text" name="quantity" size="2" value="135" />
    <input type="hidden" name="product_id" size="2" value="42" />
    &nbsp;<input type="button" value="add to cart" class="button-cart" class="button" />
  </div>

这是我的 jQuery 脚本

 jQuery('.button-cart').each(function() {
  jQuery(this).live('click',function() {
    console.log(jQuery(this));
    var s= jQuery(this).siblings('input[type="text"]');
    console.log(s);
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('.product-info input[type=\'text\'],.product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
        dataType: 'json',
        success: function(json) {
            $('.success, .warning, .attention, information, .error').remove();

            if (json['error']) {
                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                    }
                }
            } 

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                //$('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({ scrollTop: 0 }, 'slow'); 
                setTimeout(opencartpage(),1000);
            }   
        }
    });  
  });
})

现在在这里您可以看到jQuery data .product-info input[type=\'text\']已定义。在那个地方,它正在获取所有input type text data. 以同样的方式,我想定义 jQuery(this).siblings('input[type="text"]')哪个将获取当时仅选定按钮的输入值,为此我this在 ajax 数据中的类购物车中使用。所以有人可以告诉我该怎么做吗?

4

1 回答 1

0

我认为您对“添加到购物车”按钮功能有疑问。该问题可以通过以下方式轻松解决。

单击此处获取解决方案。

于 2013-12-02T06:33:16.533 回答