我知道问题出在哪里,但我不知道有什么解决办法。
我有4份文件,比如说。index.php文件、ajax.php文件、多-ajax.php文件和我的custom.js文件。
在我的索引文件中,我有一个<div id="content"></div>元素,它将从ajax.php文件中加载内容。现在,假设在这个ajax.php文件中,我有以下<div id="moreContent"></div>,现在在我的custom.js文件中,在index.php和ajax.php文件中都包含了以下jquery代码:
$(document).ready(function(){
$("#select-menu").change(function(){
$("#content").load('ajax.php');
});
$("#select-menu-two").change(function(){
$("#moreContent").load('more-ajax.php');
});
});现在,这是可行的,但是我可以看到,我正在进行的每个调用都会一次又一次地调用custom.js文件,导致请求呈指数增长。解决这一问题的一个方法是将custom.js从ajax.php中删除。但是..。然后#select-menu的第二个ajax函数不再工作,我无法从更-ajax.php加载内容。
有更好的方法吗?我能看到代码的一个例子吗?
谢谢你,SC
发布于 2014-06-13 06:23:58
避免为元素添加多个类似的均衡器,方法是先删除先前添加的均衡器:
$(document).ready(function(){
$("#select-menu").off('change');
$("#select-menu").change(function(){
$("#content").load('ajax.php');
});
$("#select-menu-two").off('change');
$("#select-menu-two").change(function(){
$("#moreContent").load('more-ajax.php');
});
});https://stackoverflow.com/questions/24198839
复制相似问题