【发布时间】:2012-02-18 07:32:07
【问题描述】:
所有, 我有一些显示的链接,如果有人喜欢它,有人可以单击该链接,然后我基本上将它分配给另一个带有删除链接的 div,以便可以将其删除。这是.post的代码
jQuery.post("save_song.php", { song_id: song_id, love_like_hate: "love" },
function(response) {
if(response.responseText1=="too_many"){
alert(response.responseText2);
}else if(response.responseText1=="already_selected"){
alert(response.responseText2);
}else{
alert(response.responseText2);
jQuery("#div_song_id_"+song_id).html(response.responseText1);
jQuery("#love_it").append(response.responseText2);
jQuery("#current_count_love").html(response.responseText3);
if(response.responseText4=="remove_initial"){
jQuery("#love_none").hide();
}
}
}, "json");
这是发送回页面的 save_song.php 页面:
echo json_encode(array(
'responseText1' => 'Youve added '.$artist_name.' - '.$track_name.' to your '.$love_like_hate.' list!',
'responseText2' => '<div id="div_added_song_id_'.$song_id.'" style="padding:0 0 0 10px; "><span style="display:inline-block; width:200px;">'.$artist_name.'</span><span style="display:inline-block; width:400px;">'.$track_name.'</span><span style="display:inline-block; width:100px;">'.$track_time.'</span><span style="display:inline-block; width:100px;"><a href="#" class="remove_song" id="delete_'.$song_id.'">Remove</a></span></div>',
'responseText3' => $resultrowschecktotal
));
我的问题是,当 response.responseText2 附加到我的 div 时,.remove_song 的 jquery 不起作用,它基本上只是使用链接并尝试执行 #。这是 remove_song 的代码:
jQuery(".remove_song").click(function(){
event.preventDefault();
song_id = jQuery(this).attr("id");
song_id = song_id.split("_");
song_id = song_id[1];
var answer = confirm("Do you want to remove this song?")
if (answer){
jQuery.post("delete_song.php", { song_id: song_id },
function(response) {
jQuery("#div_added_song_id_"+song_id).hide();
jQuery("#current_count_"+response.responseText2).html(response.responseText1);
}, "json");
}
});
由于在 DOM 完成时没有加载新附加的链接,我如何仍将其用于新附加的链接?
【问题讨论】: