jQuery $(“.class”).click(); - multiple elements, click event once

前端 未结 18 1714
借酒劲吻你
借酒劲吻你 2020-11-29 00:01

I have multiple classes on a page of the same name. I then have a .click() event in my JS. What I want to happen is the click event only happen once, regardless of multiple

18条回答
  •  余生分开走
    2020-11-29 00:39

    Could we see your click handler? You're attaching 5 listeners to 5 different elements. However, when the user clicks on the element, only one event is fired.

    $(".addproduct").click(function(){
      // Holds the product ID of the clicked element
      var productId = $(this).attr('class').replace('addproduct ', '');
    
      addToCart(productId);
    });
    

    If this solution doesn't work I'd like to look at your click handler.

提交回复
热议问题