javascript - 实现通用的ajax方法

标签 javascript ajax jsf primefaces

有没有办法实现一个通用的ajax方法,该方法将为我页面中的每个链接执行?

我的意思是执行像 checkCarousel() 这样的 js 函数,但不需要为我的项目中的每个链接在“oncomplete”ajax 属性上定义它

<p:commandLink action="#{someBean.active}" 
      update="@all" oncomplete="checkCarousel();" >
      <img width="70" src="resources/images/logo.png"                          
         alt="LOGO" style="border: none;"/>
 </p:commandLink> 

谢谢!

最佳答案

以下是简单的 Google 搜索得到的结果:

PrimeFaces uses under the covers jQuery to deal with ajax requests. So, your best bet is to hook on the generic $.ajaxComplete() handler.

$(document).ajaxComplete(function(event, xhr, options) {
    var $source = $("[id='" + options.source + "']");

    if ($source.hasClass("myCommandButton")) {
        // ...
    }
});

The $source represents the jQuery object of the original HTML DOM element on which the ajax action was been triggered, which is in case of this particular example the itself. This offers you the possibility to delegate it further to the desired handler by e.g. examining element's class.

Source

关于javascript - 实现通用的ajax方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285857/

相关文章:

css - Primefaces googlemap 不呈现

javascript - 链接结束后返回一个对象

php - 如何将 php 中的这个字符串 var 传递给 JS 函数?

javascript - ajax 将多部分表单数据上传为 json 对象

javascript - ajax 提交带有漂亮 URL 的请求

jsf - 你如何使用JSTL?

javascript - child 继承 parent 的 z-index

javascript - 使用 jquery 从另一个元素获取输入的 id

javascript - 动态 html 表单数据 AJAX 重定向到 google 表格错误

jsf - 预选项目 h :selectManyCheckbox