[問題]用jquery ajax帶出來的元素用jquery給事件無效

看板Web_Design作者 (......)時間11年前 (2014/11/03 10:32), 編輯推噓4(401)
留言5則, 4人參與, 最新討論串1/1
我有一個頁面一刷新會用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> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.200.225 ※ 文章網址: http://www.ptt.cc/bbs/Web_Design/M.1414981933.A.C75.html

11/03 11:14, , 1F

11/03 12:19, , 2F
1.用樓上說的on,把事件綁在固定不變的元素上
11/03 12:19, 2F

11/03 12:20, , 3F
2.或是在ajax的success時再綁事件
11/03 12:20, 3F

11/03 20:23, , 4F
我覺得本版的問題範本裡面可以教使用者把code貼jsfiddle
11/03 20:23, 4F

11/24 12:55, , 5F
試試看...delegate?
11/24 12:55, 5F
文章代碼(AID): #1KLkajnr (Web_Design)