如何将Jquery日期选择器中的日期传递给div元素的参数?

时间:2016-04-19 17:07:27

标签: jquery html datepicker shopify

所以我正在尝试创建一个Shopify购买按钮,该按钮链接到特定日期的产品。我需要将所选日期发送到产品手柄,然后让shopify按钮显示该产品,或者说产品不存在。这就是我所拥有的:

<html>
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">    <head>
    </head>
    <body>
    <div id="buttonId" data-embed_type="product" data-shop="maritix.myshopify.com" data-product_name="Ferry Riide" data-product_handle="ferry-riide" data-has_image="false" data-display_size="compact" data-redirect_to="cart" data-buy_button_text="Add to cart" data-buy_button_out_of_stock_text="Out of Stock" data-buy_button_product_unavailable_text="Unavailable" data-button_background_color="7fb466" data-button_text_color="ffffff" data-product_modal="false" data-product_title_color="000000" data-next_page_button_text="Next page">
    </div>



    <noscript><a href="https://maritix.myshopify.com/cart/17754591620:1" target="_blank">Buy Ferry Ride</a></noscript>
    <form>
        <p id="date"><input type="text" id="datepicker"></p>
    </form>
    </body>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script type="text/javascript">

    document.getElementById('ShopifyEmbedScript') || document.write('<script type="text/javascript" src="https://widgets.shopifyapps.com/assets/widgets/embed/client.js" id="ShopifyEmbedScript"><\/script>');
  </script>
  <script>
      $(function() {
        $( "#datepicker" ).datepicker();
        var date = $("#date").text;
        $("#buttonId").data-product_handle = date;
        $("#buttonId").data-product_name = date;
      });
  </script>

</html>

对不起,如果这是一团糟,我在javascript / html中非常生疏。

1 个答案:

答案 0 :(得分:0)

你几乎拥有它

 $("#buttonId").data-product_handle = date;
 $("#buttonId").data-product_name = date;

应该是

 $("#buttonId").data('product_handle', date);
 $("#buttonId").data('product_name' , date);

 $("#buttonId").attr('data-product_handle', date);
 $("#buttonId").attr('data-product_name' , date);

希望有所帮助