2017-07-15 75 views
0

所以我用ajax加載我的網頁,一切都用下面頁面內容 - 事件發射多次

$(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}); 
     } 
    }); 
}) 

回答

0

javascriptjava,你需要觸發事件,一次,更換

$(document).on("click", "#soldaddbtn", function(){ 

$("#soldaddbtn").on("click", function(){ 
+0

該工作的一些元素,但不是一個動態創建..任何幫助表示讚賞男人! –

+0

你有沒有在每個頁面內容上有$(document).on(「click」,「#soldaddbtn」,...' – ewwink

0

定義一個全局變量並檢查它的存在。作爲對()

將click事件綁定,因此有可能是事件是有界的多次的情況:

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 
0

我沒有完全理解你的問題,但嘗試使用下面的代碼。因此解除綁定click事件然後重新綁定它。

$("#soldaddbtn").off("click").on("click", function(){});