【问题标题】:jQuery take value for input[type ="text"] in jQuery data with defined variablejQuery 为带有定义变量的 jQuery 数据中的 input[type ="text"] 取值
【发布时间】:2012-11-19 08:37:30
【问题描述】:

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

  <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"]'),它将获取当时仅选定按钮的输入值,为此我在ajax数据的类购物车中使用this。那么有人可以告诉我该怎么做吗?

【问题讨论】:

  • 你的问题不清楚。
  • 我很确定您必须将 选择器live 一起使用,而不仅仅是 DOM 元素。

标签: jquery jquery-selectors


【解决方案1】:

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

Click Here For Solution.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2016-10-16
    相关资源
    最近更新 更多