2017-08-25 84 views
0

我有一個問題,jQuery的ajax。當我用ajax添加一個新元素時,我無法做任何事情。這裏是我的代碼:jquery不能工作機智剛剛通過ajax添加元素

$(".yorum_input").keypress(function(e) { 

    if (e.keyCode == 13) { 
    e.preventDefault(); 
    var alt_id = $(this).attr('yorum_id'); 
    var val = $(this).val(); 
    var yazi = $(this).attr('yazi_id'); 
    $(this).val(""); 

    $.ajax({ 
     url: url, 
     type: "post", 
     data: { 
     "alt_id": alt_id, 
     "icerik": val, 
     "yazi": yazi 
     }, 
     success: function(cevap) { 
     console.log(cevap); 
     cevap1 = cevap.split('***'); 
     if (cevap1[0] == "succ") { 
      if (cevap1[1] == "1") { 
      if (cevap1[8] == "0") { 
       $("#yorumlar").append("<div class='box-comment' yorum_id='" + cevap1[2] + "'>" + 
       "<!-- User image -->" + 
       "<a href='" + cevap1[3] + "'>" + 
       "<img class='img-circle img-sm' src='" + cevap1[4] + "' alt='User Image'>" + 
       "</a>" + 
       "<div class='comment-text'>" + 
       "<span class='username'>" + 
       "<a href='" + cevap1[3] + "'>" + cevap1[5] + "</a>" + 
       "<span class='text-muted pull-right'>" + cevap1[6] + "</span>" + 
       "</span><!-- /.username -->" + 
       cevap1[7] + 
       "</div>" + 
       "<!-- /.comment-text -->" + 
       "</div>" + 
       "<div class='alt_yorumlar' id='" + cevap1[2] + "'></div><div class='box-comment' style='margin-left:50px;'>" + 
       "<img class='img-responsive img-circle img-sm' src='" + cevap1[4] + "' alt='Alt Text'>" + 
       "<!-- .img-push is used to add margin to elements next to floating images -->" + 
       "<div class='img-push'>" + 
       "<input type='text' yorum_id='" + cevap1[2] + "' yazi_id='" + cevap1[9] + "' class='form-control input-sm yorum_input' placeholder='Yorumunuzu yazın!'>" + 
       "</div></div>"); 
      } else { 
       $("div[class='alt_yorumlar'][id='" + cevap1[8] + "']").append("<div class='box-comment' style='margin-left:50px;' yorum_id='" + cevap1[2] + "'>" + 
       "<!-- User image -->" + 
       "<a href='" + cevap1[3] + "'>" + 
       "<img class='img-circle img-sm' src='" + cevap1[4] + "' alt='User Image'>" + 
       "</a>" + 
       "<div class='comment-text'>" + 
       "<span class='username'>" + 
       "<a href='" + cevap1[3] + "'>" + cevap1[5] + "</a>" + 
       "<span class='text-muted pull-right'>" + cevap1[6] + "</span>" + 
       "</span><!-- /.username -->" + 
       cevap1[7] + 
       "</div>" + 
       "<!-- /.comment-text -->" + 
       "</div>"); 
      } 
      } 
     } 
     } 
    }); 
    } 
}); 

當我發表新評論,AJAX進入我的PHP代碼,如果該評論可以成功地加入到我的數據庫,它爲評論返回所需的值。然後,我創建一個包含輸入的新DOM元素。但我無法選擇新的輸入。 請有人幫我:D我需要這個爲我的網站的意見。

回答