Java 使用ajax加载页面内容-事件多次触发
因此,我正在使用ajax加载我的页面,使用下面的命令,一切都正常工作Java 使用ajax加载页面内容-事件多次触发,java,jquery,html,Java,Jquery,Html,因此,我正在使用ajax加载我的页面,使用下面的命令,一切都正常工作 $( document ).ready(function() { menuLoad('auction'); }); function menuLoad(page){ $.ajax({ type: 'post', url: '/content/'+page+'/'+page+'.php', async : false, success: f
$( document ).ready(function() {
menuLoad('auction');
});
function menuLoad(page){
$.ajax({
type: 'post',
url: '/content/'+page+'/'+page+'.php',
async : false,
success: function (response) {
location.hash = page;
$("#contentLoad").html(response);
}
})
};
但是,如果我多次尝试加载同一页面,所有按钮单击事件将触发多次。。我知道,每次我加载内容时,我都会将一个新事件重新分配给按钮,而该按钮已有一个事件,当用户单击该按钮时,该事件会触发。。但如何解决这个问题呢
在页面内容中,按钮如下所示
<input type="button" value='New Sold' id='soldaddbtn' >
$( document ).on("click", "#soldaddbtn", function(){
$.ajax({
type: "POST",
url: "/content/sold/loadSoldAdd.php",
dataType: 'html',
success: function(response){
bootbox.dialog({message: response});
}
});
})
它的javascript不是java,您需要触发一次事件,替换
$( document ).on("click", "#soldaddbtn", function(){
与
定义一个全局变量并检查其存在性
if(!soldaddbtnClickEventisAdded) { // check if the variable is defined file is already loaded
$( document ).on("click", "#soldaddbtn", function(){
$.ajax({
type: "POST",
url: "/content/sold/loadSoldAdd.php",
dataType: 'html',
success: function(response){
bootbox.dialog({message: response});
}
});
});
}
var soldaddbtnClickEventisAdded = true; // add a global variable
我不完全理解您的问题,但请尝试使用以下代码: As on将绑定click事件,因此可能存在多次绑定事件的情况。因此,请取消绑定click事件,然后再次绑定它
$("#soldaddbtn").off("click").on("click", function(){});
对某些元素有效,但对动态创建的元素无效。。任何帮助都是感激的人!你有$document.onclick,soldaddbtn,。。。每一页的内容?
$("#soldaddbtn").off("click").on("click", function(){});