Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQueryAjaxSuccess_Jquery_Ajax - Fatal编程技术网

jQueryAjaxSuccess

jQueryAjaxSuccess,jquery,ajax,Jquery,Ajax,我将jQuery与ajaxSuccess一起使用 我有一个预加载的HTML 我的jQuery代码是: abc(); function abc() { $('.clickme').click(function() { alert(''); // some ajax code and it will give some html // }); $('.secondClass').click(function() { //some ajax code/// }); } 以上代

我将jQuery与
ajaxSuccess
一起使用

我有一个预加载的HTML

我的jQuery代码是:

abc();

function abc() {
$('.clickme').click(function() {
alert('');
// some ajax code and it will give some html //
});
 $('.secondClass').click(function()  {
     //some ajax code///

  });

}
以上代码都在文件准备功能中。 现在,当我使用
ajaxSuccess
函数时:

$(document).ajaxSuccess(function( event, request, settings ) { abc(); }
现在,当我单击
.clickme
类时,它正在向服务器发送多个请求


如何解决这个问题?

这是因为每当发出ajax请求时,您都在向目标元素添加新的处理程序

我认为您这样做是为了处理动态添加的元素,而不是使用ajax回调

在这种情况下,不需要使用
abc
方法


演示:,

无需在方法abc()中写入单击事件。直接写在文档里面,准备好了就可以实现上面的目标了

 $(document).ready(function() {
     $(document).on('click', '.clickme', function() {
         alert('');
         // some ajax code and it will give some html //
     });
     $(document).on('click', '.secondClass', function() {
         //some ajax code///

     });
 });

这不会触发多个请求。。希望它能有所帮助

这是因为每当发出ajax请求时,您都会向目标元素添加一个新的处理程序;
 $(document).ready(function() {
     $(document).on('click', '.clickme', function() {
         alert('');
         // some ajax code and it will give some html //
     });
     $(document).on('click', '.secondClass', function() {
         //some ajax code///

     });
 });