简体   繁体   English

jQuery使用定义的变量为jQuery数据中的input [type =“ text”]输入值

[英]jQuery take value for input[type =“text”] in jQuery data with defined variable

I have my markup like this. 我有这样的标记。 Now here you can see every button has same class named button. 现在,在这里您可以看到每个按钮都具有名为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>

Here is my jQuery script 这是我的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);
            }   
        }
    });  
  });
})

Now here you can see in jQuery data .product-info input[type=\\'text\\'] is defined. 现在,您可以在jQuery data .product-info input[type=\\'text\\']看到jQuery data .product-info input[type=\\'text\\']已定义。 In that place it is taking the data for all the input type text data . 在那个地方,它为所有input type text data In the same way I want to define jQuery(this).siblings('input[type="text"]') which will take the input value for only selected button at that time for this I am using this inside class cart in ajax data .So can some one tell me how to do this? 以同样的方式,我想定义jQuery(this).siblings('input[type="text"]') ,该方法将只接受当时所选按钮的输入值,因此我正在ajax中使用this内部购物车数据。那么有人可以告诉我该怎么做吗?

I think you have problem with Add to Cart button functionality. 我认为您对“添加到购物车”按钮功能有疑问。 The problem can be easily resolved via. 该问题可以通过轻松解决。

Click Here For Solution. 单击此处获取解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM