[问题]用jquery ajax带出来的元素用jquery给事件无效

楼主: supercygnus (......)   2014-11-03 10:32:11
我有一个页面一刷新会用jquery ajax带一些html元素放到网页中
这些带出来的元素有办法再给他按钮事件吗?
试试好像没效
该如何改进
<script type="text/javascript">
$(function(){
$.MyExtend.showOnline();
$("input[name='hh']").click(function(){alert("dd");});
});
$.MyExtend={
showOnline:function(){
alert("enter!!");
$.ajax({
type:"GET",
url:"online.jsp",
//dataType:text,
success:function(msg){
//alert("suucess");
$("#online").html(msg);
},
error:function(){
alert("error!!");
}
});
},
showContent:function(){
$.ajax({
type:"GET",
url:"online.jsp",
success:function(msg){
//alert("suucess");
$("#online").html(msg);
},
error:function(){
alert("error!!");
}
});
},
sendMessage:function(){
if($("input[name='name']").val()==''){
alert("请输入对象!!");
return false;
}
if($("input[name='talk_content']").val()==''){
alert("发送讯息不能为空!!");
return false;
}
var from=$("input[name='name']").val();
var to=$("input[name='to']").val();
var content=$("input[name='talk_content']").val();
//var from=$("input[name='name']").val();
$.ajax({
type:"POST",
dataType:"jSon",
url:"MessageServlet?action=sendMessage",
success:function(msg){
//alert("suucess");
$("#online").html(msg);
},
error:function(){
alert("error!!");
}
});
},
selectOne:function(){
//var username=$("#selectone").val();
//$("input[name='to']").val(username);
//alert("hello");
$(this).click(function(){
alert("hello");
});
}
}
</script>
作者: Y78 (Y78)   2014-11-03 11:14:00
作者: Kenqr (function(){})()   2014-11-03 12:19:00
1.用楼上说的on,把事件绑在固定不变的元素上2.或是在ajax的success时再绑事件
作者: davidsky (Alive)   2014-11-03 20:23:00
我觉得本版的问题范本里面可以教使用者把code贴jsfiddle
作者: chikasa (结束就是一个开始)   2014-11-24 12:55:00
试试看...delegate?

Links booklink

Contact Us: admin [ a t ] ucptt.com