如何从以下代码中读取data-product_id [英] How to read the data-product_id from the following code

查看:23
本文介绍了如何从以下代码中读取data-product_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以在 woocommerce 中捕获我将产品添加到购物车的事件.

I have a script that in woocommerce catches me an event of adding a product to my cart.

    <script>
        (function($){
           $('body').on( 'added_to_cart', function(e, fragments, cart_hash, this_button){
              console.log('this_button:',this_button);
           });
        })(jQuery);
    </script>

console.log(this_button);

我得到了这个答案,并在其中看到了我想要的 product_id

I get this answer and in it I see my desired product_id

this_button: 
{…}
​
0: <a class="button product_type_simple add_to_cart_button ajax_add_to_cart added" href="/Blog/produkt/klawwa/?add-to-cart=14" data-quantity="1" data-product_id="14" data-product_sku="" aria-label="Dodaj "Fajna flaszka" do koszyka" rel="nofollow">
​
context: <a class="button product_type_simple add_to_cart_button ajax_add_to_cart added" href="/Blog/produkt/klawwa/?add-to-cart=14" data-quantity="1" data-product_id="14" data-product_sku="" aria-label="Dodaj "Fajna flaszka" do koszyka" rel="nofollow">
​
length: 1
​
selector: ""
​
__proto__: Object { jquery: "1.12.4", constructor: n(), length: 0, … }

现在的问题是如何从这个序列中找到 data-product_id 的值.这个问题实际上与 woocomerce 无关,而是与 JS 本身有关.在此先感谢您的帮助.

And now the question how to find the value of data-product_id from this sequence. The question is not actually related to woocomerce but to JS itself. Thank you in advance for all your help.

推荐答案

您可以通过将其添加到您的 .click() 函数来检索值.

You can retrieve the value by adding this to your .click() function.

$(this).data('product_id');

尝试:

 <script>
        (function($){
           $('body').on( 'added_to_cart', function(e, fragments, cart_hash, this_button){
              console.log($(this_button).data('product_id'));
           });
        })(jQuery);
    </script>

这篇关于如何从以下代码中读取data-product_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆