2013-06-19 25 views
1

要做第二個按鈕,我需要做些什麼?第一個按鈕運行平穩,但第二個不是,如果你點擊test_link它會顯示在第二個按鈕,其中創建由JavaScript提交按鈕以通過ajax發佈數據,由createhtml製作而不工作

<script src="http://localhost/googledrive/www/ci/2.1.3/social/assets/jquery/jquery-1.7.1.js"></script> 
    <script type="text/javascript" > 
    $(document).ready(function() 
    { 
     $(".comment_button").click(function(){ 
      $.ajax({ 
       type: "POST", 
       dataType : "json", 
       url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test", 
       data: { name: "John", time: "2pm" }, 
       success : function(cont) { 
        if(cont){ 
         $.each(cont, function(key, val) { 
          alert(key+" "+val); 
         }); 
         } 
        }    
      }); 
      return false;    
     }); 

     $(".comment_link").click(function(e){ 
     e.preventDefault(); 
     var element = $(this); 
     var id = element.attr("post_id"); 
     $("#"+id).html('<input><button class="comment_button">Second button, not work</button>'); 
     return false; 
     });   
    }); 
    </script> 



     <form method="post" action="http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test/2"> 
     <button class="comment_button">first button, this_button works</button> 
     </form> 
     <div id="7"></div> 
     <a href="http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test/2" class="comment_link" post_id="7">test_link</a> 
     <br> 
     php code : <?php echo json_encode($_POST);?> 
+0

看看到了'。對()' http://api.jquery.com/on/它應該是你在找什麼。由於您的點擊在HTML實際製作之前已準備就緒,因此您使用的舊點擊甚至不知道該元素存在。 .on()類型的刷新元素來尋找他們 – ntgCleaner

回答

0

你設置.click()方法按鈕添加到DOM之前,你可以重新定義你的JavaScript,如:

$(document).ready(function() { 
    $(".comment_link").click(function (e) { 
     e.preventDefault(); 
     var element = $(this); 
     var id = element.attr("post_id"); 
     $("#" + id).html('<input><button class="comment_button">Second button, not work</button>') 
     .children(".comment_button").click(function() { 
      $.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test", 
       data: { 
        name: "John", 
        time: "2pm" 
       }, 
       success: function (cont) { 
        if (cont) { 
         $.each(cont, function (key, val) { 
          alert(key + " " + val); 
         }); 
        } 
       } 
      }); 
      return false; 
     }); 
     return false; 
    }); 
}); 

鏈接的方法.click()添加按鈕後,看着它的母公司。 或者:

$(document).ready(function() { 
    $(".comment_link").click(function (e) { 
     e.preventDefault(); 
     var element = $(this); 
     var id = element.attr("post_id"); 
     $("#" + id).html('<input><button class="comment_button">Second button, not work</button>') 
      .children(".comment_button").click(commentButtonClicked); 
     return false; 
    }); 

    function commentButtonClicked() { 
     $.ajax({ 
      type: "POST", 
      dataType: "json", 
      url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test", 
      data: { 
       name: "John", 
       time: "2pm" 
      }, 
      success: function (cont) { 
       if (cont) { 
        $.each(cont, function (key, val) { 
         alert(key + " " + val); 
        }); 
       } 
      } 
     }); 
     return false; 
    } 
}); 

移動你的Ajax調用單獨的非匿名函數,從前面的例子中鏈接像.click()事件調用它...

+1

謝謝你,你的代碼是驚人的... –

+0

@HermannGolden我很高興它幫助:) – DarkAjax