简体   繁体   English

如何获取单击按钮的数据属性值?

[英]How can I get data attribute value of clicked button?

I want to get data attribute value of clicked button. 我想获取单击按钮的数据属性值。 I need to use it When I submit an ajax form. 提交ajax表单时需要使用它。 How can I get that ? 我该怎么办?

My button (There are as many buttons as the number of products) 我的按钮(按钮数量与产品数量一样)

<input value="Add To Cart" type="button" class="btn btn-success btn-sm btn-block add-to-card-button" data-id="{{$product->id}}" style="margin-bottom:10px">

Javascript codes; Javascript代码;

const cartButton = document.getElementsByClassName('add-to-card-button');

if (cartButton) {
    Array.from(cartButton).forEach(function(element) {
        element.addEventListener('click', addToCard);
    });
   }

function addToCard() {

    $.ajax({
        url: "/auth/check",
        context: document.body,
        success: function(status){
           if (status === "Auth")
           {
               addToCardForUsers();
           } else {
               addToCardForGuests()
           }
        }
    });

}

function addToCardForUsers() {

    $.ajax({
        type: 'POST',
        url: "/cart/add-product/"+ cartButton.dataset.id,
        context: document.body,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function(){
                if(countProduct.text() == null) {
                    countProduct.text(1);
                } else {
                    countProducts =  Number(countProduct.text());
                    countProducts++;
                    countProduct.text(countProducts);
                }              
                Flash.success("Product succesfully added to your cart");
        }
    });
}
<input value="Add To Cart" type="button" class="btn btn-success btn-sm btn-block add-to-card-button" data-id="{{$product->id}}" style="margin-bottom:10px">

To get the data-id attribute value from above code on button click event: 要从上述按钮单击事件中的代码获取data-id属性值:

$(this).data('id')

JSFiddler Demo: https://jsfiddle.net/682xdbkv/1/ JSFiddler演示: https ://jsfiddle.net/682xdbkv/1/

Change the way you bind your event listner to this, 更改将事件列表器与此绑定的方式,

$('.add-to-card-button').on('click', function () {
    addToCard();
    alert($(this).data('id'));
});

You should use: 您应该使用:

function addToCard(e) {
    alert($(e.target).data('id'))
}

What you could do is grab the id in addToCart and pass that as a parameter to addToCardForUsers where you can use it in your endpoint string. 您可以做的是在addToCart获取id并将其作为参数传递给addToCardForUsers ,您可以在端点字符串中使用它。 And since you're using jQuery, perhaps use it to pick up the buttons and assign the function to them on the click event. 并且由于您使用的是jQuery,也许可以使用它来拾取按钮,并在click事件上将功能分配给它们。

$('add-to-card-button').on('click', addToCart);

function addToCard() {
  const id = $(this).data('id');
  $.ajax({
    url: "/auth/check",
    context: document.body,
    success: function(status) {
      if (status === "Auth") {
        addToCardForUsers(id);
      } else {
        addToCardForGuests()
      }
    }
  });
}

function addToCardForUsers(id) {
  $.ajax({
    type: 'POST',
    url: "/cart/add-product/" + id,
    ...
  });
}

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

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