简体   繁体   English

如何在 Ajax 触发元素上监听 Ajax 事件?

[英]How to listen Ajax events on an Ajax triggering element?

In my HTML document I have this code:在我的 HTML 文档中,我有以下代码:

<a id='ajax-trigger' href='...', data-one='...'>
  Trigger AJAX!
</a>

And in a external, not my own and uneditable JS library I have something like this:在外部的,不是我自己的和不可编辑的 JS 库中,我有这样的东西:

$('#ajax-trigger').on('click', function() {
  $.ajax({
    type: "POST",
    url: a.attr('href'),
    data: postData,
    dataType: 'json',
    success: function(data) {
      // handle Ajax success...
    },
    error: function(jqXHR, textStatus, errorThrown) {
      // handle Ajax error...
    }
  });
});

With the above code in place, when the <a> link is clicked then the Ajax request is triggered.有了上面的代码,当点击<a>链接时,就会触发 Ajax 请求。

Now, to further manipulating the DOM after a success Ajax response, I'm looking for a way to listen the success Ajax response local event related to the Ajax triggering element without changing the JS library source code.现在,为了在 Ajax 响应成功后进一步操作 DOM,我正在寻找一种方法来监听与 Ajax 触发元素相关的成功 Ajax 响应本地事件,而无需更改 JS 库源代码。 That is, I would like to add an Ajax success event listener for the <a> element in the HTML document without changing the original JS library.也就是说,我想在不改变原始JS库的情况下,为HTML文档中的<a>元素添加一个Ajax成功事件监听器。 Maybe, something like this (note: ajaxSuccess is just an example):也许,像这样(注意: ajaxSuccess只是一个例子):

<a id='ajax-trigger' href='...', data-one='...'>
  Trigger AJAX!
</a>

<script type="text/javascript">
  $(document).ready(function() {
    $('#ajax-trigger').on('ajaxSuccess', function() {
      // handle Ajax success in addition to the 
      // Ajax success handler in the JS library...
    });
  });
</script>

Is it possible with jQuery/JS?使用 jQuery/JS 可以吗?

Using just your code and adding pure JS - solution 2 answers your question.仅使用您的代码并添加纯 JS - 解决方案 2 回答您的问题。

Btw.: why do you need to add this event/event listener?顺便说一句:你为​​什么需要添加这个事件/事件监听器? If you want a function to be executed after your successful ajax call, all you need to do is call the function as part of your $.ajax success如果您希望在成功调用 ajax 后执行一个函数,您需要做的就是调用该函数作为 $.ajax 成功的一部分

Solution 1:解决方案1:

$('#ajax-trigger').on('click', function() {
  $.ajax({
    type: "POST",
    url: a.attr('href'),
    data: postData,
    dataType: 'json',
    success: function(data) {
      // handle Ajax success...
      myAjaxSuccessFunction();
    },
    error: function(jqXHR, textStatus, errorThrown) {
      // handle Ajax error...
    }
  });
});

function myAjaxSuccessFunction() {
    //do some stuff
}

Solution 2: fire an actual event on the element, and have a listener on the element for that kind of event.解决方案 2:在元素上触发一个实际事件,并在该元素上为这种事件设置一个侦听器。

//create the event
var myAjaxSuccessEvent = new Event('AjaxSuccessEvent');

//attach the event to the HTML element
var myHTMLelement = document.getElementById('my-element');
myHTMLelement.addEventListener('AjaxSuccessEvent', function() {
    //do something
    alert("Event fired");
});

//or attach the listener like this, in your case
$('#ajax-trigger').on('AjaxSuccessEvent', function() {
  // handle Ajax success in addition to the 
  // Ajax success handler in the JS library...
});

//fire the event after successful AJAX call
$('#ajax-trigger').on('click', function() {
  $.ajax({
    type: "POST",
    url: a.attr('href'),
    data: postData,
    dataType: 'json',
    success: function(data) {
      // handle Ajax success...
      myHTMLelement.dispatchEvent(myAjaxSuccessEvent);
      var myElement = document.getElementById('ajax-trigger');
      myElement.dispatchEvent(myAjaxSuccessEvent);
    },
    error: function(jqXHR, textStatus, errorThrown) {
      // handle Ajax error...
    }
  });
});

Solution 3: without the custom event You can't add an event listener unless there's an actual event you're subscribing to.解决方案 3:没有自定义事件除非有您订阅的实际事件,否则您无法添加事件侦听器。 If you don't want to add a custom event, you'll have to fire one of the existing events.如果您不想添加自定义事件,则必须触发现有事件之一。

//fire the event after successful AJAX call
$('#ajax-trigger').on('click', function() {
  $.ajax({
    type: "POST",
    url: a.attr('href'),
    data: postData,
    dataType: 'json',
    success: function(data) {
      // handle Ajax success...
      // use an existing event
      var getMyElement = document.getElementById('ajax-trigger');
      getMyElement.onchange();
    },
    error: function(jqXHR, textStatus, errorThrown) {
      // handle Ajax error...
    }
  });
});

$('#ajax-trigger').on('onchange', function() {
    //do something
});

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

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