【问题标题】:jQuery multple button with the same class name is taking only last value具有相同类名的jQuery多个按钮仅采用最后一个值
【发布时间】:2012-11-19 00:09:57
【问题描述】:

在下面的markup 中,您可以看到class name is same for every button。我有更多具有相同类名按钮的按钮。现在,当我单击任何按钮时,它只获取last button value 的值。在这里您可以看到last div 的值为30。所以点击每个按钮它只取最后一个值。 我的html标记是这样的

<div class="cart">
  <div>Qty:
    <input type="text" value="10" size="2" name="quantity">
    <input type="hidden" value="42" size="2" name="product_id">
    &nbsp;<input type="button" class="button-cart" value="Add to Cart">
  </div>
  <div>Qty:
    <input type="text" value="20" size="2" name="quantity">
    <input type="hidden" value="42" size="2" name="product_id">
    &nbsp;<input type="button" class="button-cart" value="Add to Cart">
  </div>
  <div>Qty:
    <input type="text" value="30" size="2" name="quantity">
    <input type="hidden" value="42" size="2" name="product_id">
    &nbsp;<input type="button" class="button-cart" value="Add to Cart">
  </div>
</div>

现在我的 jQuery 脚本是这样的

$('.button-cart').click(function() {
    $.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);
            }   
        }
    });
});

【问题讨论】:

    标签: html jquery jquery-selectors


    【解决方案1】:

    在您的代码中,您将 jQuery 选择器作为 AJAX 方法的数据。这可能没有意义,因为您想要的是名称/值对(或表单字段的其他表示形式)。

    对于我认为您想做的事情(将所有表单字段放入 AJAX 请求中),最简单的解决方案是使用 .serialize() 函数。

    如果您的所有字段都采用 id #foobar 的相同形式,那么将它们全部作为查询字符串返回将是 $('#foobar').serialize()

    【讨论】:

      【解决方案2】:

      试试这个

      $(".button-cart").click(
              function()
              {
                  var parent_node = $(this).parent("div");
                  var quantity = parent_node.find('input[name="quantity"]').val();
                  alert(quantity);
              } );
      

      this 将确定您准确点击的类

      【讨论】:

      • 好吧,我只是展示如何通过单击按钮获得价值的示例。获得价值后,您可以使用您的 ajax 并根据需要传递价值:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2022-10-25
      • 2016-05-24
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多