2012-07-15 85 views
3

我想僅在第一次點擊時調用ajax以節省帶寬,數據量非常大。下面是我的代碼僅在第一次點擊時觸發Ajax

$('.id-list').click(function() { 
$.ajax({ 
    type: "POST", 
    url: "timesheet.php?action=idlist", 
    async: false, 
    success: function(rdata) { 
      $('.id-list').removeClass('id-list') //remove the class 
    } 
})  
}) 

檢查的元素,除去class="id-list",但是當我再次點擊它,它再次觸發Ajax調用,我不希望發生的事情。

有什麼辦法可以實現這個嗎? 謝謝。

回答

3

API .onehttp://api.jquery.com/one/

附加的處理程序,爲元素的事件。處理程序每​​個元素最多執行一次。

$('.id-list').one('click', function() { 
    $.ajax({ 
    type: "POST", 
    url: "timesheet.php?action=idlist", 
    async: false, 
    success: function(rdata) { 
      $('.id-list').removeClass('id-list') //remove the class 
    } 
})  
}) 
+1

偉大的思想.... – mplungjan 2012-07-15 06:35:26

+0

@mplungjan謝謝! – genpet 2012-07-15 06:42:20

+0

@Tats_innit謝謝! – genpet 2012-07-15 06:42:42

0

http://api.jquery.com/live/

$('.id-list').live("click", function(){ 
$.ajax({ 
    type: "POST", 
    url: "timesheet.php?action=idlist", 
    async: false, 
    success: function(rdata) { 
     $('.id-list').removeClass('id-list') //remove the class 
    } 
}) 
}); 
+0

Live不推薦使用,即使它在移除類時可能會有效 – mplungjan 2012-07-15 06:34:50

2

http://api.jquery.com/one/

描述:附加的處理程序,爲元素的事件。處理程序每​​個元素最多執行一次。

$('.id-list').one("click",function() { 
    $.ajax({ 
    type: "POST", 
    url: "timesheet.php?action=idlist", 
    async: false, 
    success: function(rdata) { 
             $('.id-list').removeClass('id-list') //remove the class 
    } 
    })       
}) 
相關問題