Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向购物车发送标记和值时出现的问题_Javascript_Jquery_Ajax_Shopify - Fatal编程技术网

Javascript 向购物车发送标记和值时出现的问题

Javascript 向购物车发送标记和值时出现的问题,javascript,jquery,ajax,shopify,Javascript,Jquery,Ajax,Shopify,我目前正在shopify的一个网站上工作。我已经设置了我希望使用的订单,目前一切正常,但是我还需要添加一个客户文本字段,将团队名称传递到购物车 Shopify.itemsToAdd = []; Shopify.addItemstoTheCart = function() { if (Shopify.itemsToAdd.length) { var item = Shopify.itemsToAdd.pop(); $.ajax({ url: '/cart/add',

我目前正在shopify的一个网站上工作。我已经设置了我希望使用的订单,目前一切正常,但是我还需要添加一个客户文本字段,将团队名称传递到购物车

Shopify.itemsToAdd = [];
Shopify.addItemstoTheCart = function() {
  if (Shopify.itemsToAdd.length) {
    var item = Shopify.itemsToAdd.pop();
    $.ajax({
      url: '/cart/add',
      dataType: 'json',
      type: 'post',
      data: item,
      success: Shopify.addItemstoTheCart,
      error: Shopify.addItemstoTheCart
    });
  }
  else {
    window.location.href = '/cart';
  }
};
jQuery(function($) {
  $('table .quantity:first').focus();
  $('.add-to-cart-order-form').click(function() {
    $('.add-to-cart-order-form').addClass('disabled').attr('disabled','disabled');
    // Resetting.
    Shopify.itemsToAdd = [];
    $('.quantity').each(function() {
      var quantity = parseInt($(this).val(), 10);
      if (this.checked) {
        Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity} );
      }
    });     
    if (Shopify.itemsToAdd.length) {
      Shopify.addItemstoTheCart();
    }
    else {
      alert('All quantities are set to zero.'); 
      $('.add-to-cart-order-form').removeAttr('disabled').removeClass('disabled');
    }
  });
});
因此,我需要从以下输入发送名称name作为变量,从客户发送输入作为变量数据

<input type="text" class="tname" name="properties[tname]">
发送到购物车的数据是id、数量和属性[tname]

我试着输入这个,但没有成功

Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties[tname]: "walker"} );
将“walker”列为变量的测试数据。我相信[]会继续出错,但他们必须在那里,shopify系统才能确认列表并将其填充到购物车中

如果你能提供任何帮助,我将不胜感激

编辑

好的,我可以获取要在购物车中发布的团队名称,但我只能获取要发送的第一个文本字段。我已将文本字段id标记与复选框数据id标记对齐。我的函数只对第一对有效。之后,所有其他内容都会被发送到购物车,而不包含团队名称

jQuery(function($) {
  $('table .quantity:first').focus();
  $('.add-to-cart-order-form').click(function() {
    $('.add-to-cart-order-form').addClass('disabled').attr('disabled','disabled');
    // Resetting.
    Shopify.itemsToAdd = [];
    $('.quantity').each(function() {
      var quantity = parseInt($(this).val(), 10);
      var did = $(this).attr('data-id')
      var idname;
      if (did == $('.tname').attr('id')) {
        var idname = $('.tname').val();
      };
      if (this.checked) {
        Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties: { tname: idname}} );
      }
    });     
    if (Shopify.itemsToAdd.length) {
      Shopify.addItemstoTheCart();
    }
    else {
      alert('All quantities are set to zero.');
      $('.add-to-cart-order-form').removeAttr('disabled').removeClass('disabled');
    }
  });
});
应该是:

Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties: { tname: "walker"}} );

上面看到的另一个问题这应该是一个单独的问题,看起来您需要更多的jQuery:
var idname=$('.tname[id=“'+did+']”)val()
var idname=$(“#”+did).val()效果很好,非常感谢。我不敢相信事情竟那么简单@晚上
Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties: { tname: "walker"}} );