I am trying to make an alert window fire when adding specific products to cart in WooCommerce. For this I am adding a click function to the add to cart button, that fires on ajaxComplete. However, the ajaxComplete seems to make it fire multiple times. How can I prevent this?

当我在WooCommerce中添加特定的产品时,我正在尝试发出警告窗口。为此,我向add to cart按钮添加了一个click函数,它在ajaxComplete上启动。然而,ajaxComplete似乎让它火了很多次。我该如何预防呢?

jQuery('#bvbutton').click(function() {
  jQuery( document ).ajaxComplete(function() {
    alert("testing!");
  });
});

EDIT: Per request here is a more thorough explanation: The ajax call is made from within WooCommerce and I would prefer not editing this. What I am trying to do is giving the buttons of the specific products a unique id, and making a click on a button with this id trigger an alert box.

编辑:这里的每个请求都有一个更全面的解释:ajax调用来自WooCommerce,我不希望编辑它。我要做的是给特定产品的按钮一个唯一的id,然后用这个id单击一个按钮,就会触发一个警报框。

The whole point is that items from within in a specific category, a, is supposed to give 10 % percent discount on products from category b. When adding a product from category a, the alert box is supposed to fire and inform about discount on category b. Hence I give these buttons a specific id, and would like to make a click event targeting these id's. When clicked, I need to await the ajax call that adds item to cart and then fire alert box.

事情的关键在于从内部项目在一个特定的类别,一个,应该是给10%折扣的产品类别b。当添加一个产品类别,警告框应该是火和通知关于折扣类别b。因此我给这些按钮一个特定的id,并单击事件针对这些id。单击时,我需要等待向cart添加项目的ajax调用,然后启动警报框。

Hope it makes sense now

希望现在讲得通。

2 个解决方案

#1


2

$.ajaxComplete is global. This handler will be added to all AJAX requests as many times as is called. In your case each time #bvbutton is clicked a another handler instance is added globally.

美元。ajaxComplete是全球性的。这个处理程序将被多次添加到所有AJAX请求中。在您的示例中,每当单击#bvbutton时,全局添加另一个处理程序实例。

Instead, add the handler once in the DOM ready event. Not on any specific click events.

相反,在DOM ready事件中添加处理程序一次。不针对任何特定的单击事件。

$(function() {
  jQuery( document ).ajaxComplete(function() {
    alert("testing!");
  });
});

更多相关文章

  1. 隐藏加载更多记录按钮ASP.net MVC Ajax Javascript
  2. 如何更改我的按钮文字?
  3. 单选按钮单击,隐藏/显示不工作
  4. 在Internet Explorer中使用sprited按钮和selection.createRange()
  5. 使用按钮单击按钮添加表格
  6. 具有相同名称的Mutiple按钮显示不同的div。
  7. 如何检查不包含提交按钮的HTML5表单的有效性?
  8. 当函数在单独的PHP文件中定义时,调用JavaScript函数onclick按钮事
  9. 由ajax [duplicate]生成时,formset的提交按钮不起作用

随机推荐

  1. Angular2-对象作为无线电输入值
  2. 如何在HTML视频标签中播放FLV视频?
  3. HTML5-Service Worker实现离线页面访问
  4. reload a div without reloading the who
  5. js获取html下拉框中选中值的自定义属性值
  6. html+css 图片右上角加删除叉,图片删除
  7. HTML DIV百分比宽度奇奇怪怪的间距产生了
  8. seo 优化去掉html 页面的后缀 .html
  9. form在提交时被取消:因为form没有被连接
  10. 使用表单字段值定制CSS属性。