[英]ajax append causes javascript to be executed multiple times
我在PHP中創建了一個動態表,其中一行中的每個圖像都在ID中有一個特定的URL。
例如:
當用戶單擊此圖像時,它會執行操作。 這很好用。
之后,在第二個ajax中,它應該用前一個url重新加載colorbox。 這也有效,但javascript似乎再次加載(雖然有新值)?
$('#cboxLoadedContent img[alt="markmessage"]').live('click', function(){
var returnurl = "<?php echo $_SESSION['returnpage']; ?>";
var markurl = $(this).attr('id');
alert(markurl);
// Do the action
$.ajax({
method:'GET',
url: markurl,
cache:false
});
// Reload colorbox again with previous contenturl.
$.ajax({
type: 'GET',
url: returnurl,
dataType: 'html',
cache: true,
beforeSend: function() {
$('#cboxLoadedContent').empty();
$('#cboxLoadingGraphic').show();
},
complete: function() {
$('#cboxLoadingGraphic').hide();
},
success: function(data) {
$('#cboxLoadedContent').empty();
$('#cboxLoadedContent').append(data);
}
});
});
</script>
有沒有辦法防止javascript被重新附加到colorbox? 我嘗試了一些方法(比如用DOM刪除它),但似乎沒有任何工作......
javascript可能不會被禁用,之后它應該處理一個新的URL ...
嘗試在第一行中將live
替換為one
: $('#cboxLoadedContent img[alt="markmessage"]').one('click', function(){
我不確定你使用的是哪個版本的jQuery,但是從1.7開始不推薦使用live()
,你可能想嘗試使用on()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.