我有一种情况我使用jQuery生成动态html,如
var list_of_addressbook_entries = {};
var filter = []
function save(){
var user = $("#addressbook_user").val();
var desg = $("#desg").val();
var ahtml='<div id ="user_'+user+'" class="addr_list" style=" font-size: 30px;display:block; background: none repeat scroll 0% 0% peachpuff;margin:1px 1px 1px 1px ;">'
ahtml = ahtml + '<span style="display: block; float: left; font-size: 30px; margin: 2px 24px 2px 21px;" >'+user+'</span>'
if(desg == 'M'){
ahtml = ahtml + '<span style="display: block; float: left; font-size: 30px; margin: 2px 24px 2px 21px;">Moderator</span>'
}
else{
ahtml = ahtml + '<span style="display: block; float: left; font-size: 30px; margin: 2px 24px 2px 21px;">Attendee</span>'
}
ahtml = ahtml + '<div onclick="remove_video(\''+user+'\')"><span ><img src="/UI/user/img/delet.jpeg" alt="Smiley face" height="42" width="42" /> </span></div></div>'
var ua = "'"+user+"'";
filter.push(ua);
$.ajax({
type: "GET",
url: "<?php echo SITE_URL;?>/UI/user/filterbox.php",
data: "list="+ filter+"&Where=addressbook" ,
success: function(json){
$('#addorguser').html(json);
// if(!json.error) location.reload(true);
},
error: function (xhr, textStatus, errorThrown) {
alert(json);
}
});
$("#organization").append(ahtml);
}
function remove_video(user){
alert(user);
$("#user_"+user).hide();
}
在调用save()之后,我将动态生成的html附加到div,还有一个remove_video函数,我也在动态html中定义。
但是当我点击动态div去除div时,remove_video函数没有被调用。
我也没有收到任何错误。
帮帮我。
* 更新了Html *
<div id="organization" name="organization">
</div>
<div>Address Book</div><br>
<div id="box1">
<?php
if (isset($_SESSION["user_name"])){
$get_user_name = $_SESSION["user_name"];
?>
<div id="addorguser" style="display: block; float: left;">
<select id ="addressbook_user" name="addressboook_user">
<?php
$asql = "SELECT * from demo_addressbook WHERE user_created_id IN(SELECT id FROM demo_user WHERE user_name = '$get_user_name') AND type = 1 ";
// $result = mysql_query($query);
// mysql_real_escape_string($asql);
$aresult = mysql_query($asql) or die (mysql_error());
while($arow_list=mysql_fetch_assoc($aresult)){
?>
<option value="<?php echo $arow_list['guest_name']; ?>"><?php echo $arow_list['guest_name']; ?></option>
<?php
}
?>
</select>
</div>
<select name="desg" id="desg">
<option value="A">Attendee</option>
<option value="M">Moderator</option>
</select>
<input type="button" id ="aadd" onclick="save()" name="button" value="Add"/>
</div>
<?php
}
?>
答案 0 :(得分:0)
试试这个例子:
$('#aadd').on('click', function(e){
alert('Click');
});
或者这个
$('#aadd').click(function(e){
alert('Click');
});