看板 Web_Design 關於我們 聯絡資訊
我有一個頁面一刷新會用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
Y78: http://api.jquery.com/on/ 11/03 11:14
Kenqr: 1.用樓上說的on,把事件綁在固定不變的元素上 11/03 12:19
Kenqr: 2.或是在ajax的success時再綁事件 11/03 12:20
davidsky: 我覺得本版的問題範本裡面可以教使用者把code貼jsfiddle 11/03 20:23
chikasa: 試試看...delegate? 11/24 12:55