2012-06-25 258 views
0

對,我解決了我之前無法在返回鍵上提交textarea但提出了新問題的問題。它通過ajax提交評論,然後在第一個狀態下加載它。無論我嘗試添加註釋到哪個帖子,它都會將註釋添加到頁面上的第一個狀態div,而不是執行操作的註釋。但是,如果我刷新頁面,評論顯示爲正確的狀態,所以我知道正確的ID正在提交,它只是被附加在錯誤的地方,但我不明白爲什麼。通過ajax提交textarea,並通過ajax動態加載內容

我提交代碼:

$(function() { 
    $('textarea#scat').bind('keydown', function(e) { 
     if(e.keyCode==13){ 
      var myClass = $(this).attr("class"); 
      var comment = $("textarea." + myClass).val(); 
      if (comment == "") { 
       return false; 
      } 

      if (!$.trim($("textarea." + myClass).val())) { 
       return false; 
      } 

      var cid = $("input.c_" + myClass).val(); 
      var itemid = $("input.i_" + myClass).val(); 
      var type = $("input.t_" + myClass).val(); 
      var top = $("input.l_" + myClass).val(); 

      var dataString = 'comment='+ comment + '&cid=' + cid + '&itemid=' + itemid + '&type=' + type; 

      $.ajax({ 
       type: "POST", 
       url: "addcomment.php", 
       data: dataString, 
       success: function() { 
        $('#c').load('ajax/querylc.php?oid=' + myClass); 
        $("textarea." + myClass).val(''); 
       } 
      }); 
     return false; 
     } 
    }); 
}); 

我的AJAX/querylc.php(它加載註釋並追加它說狀態)

$onuid = strip_tags(stripslashes(htmlentities(mysql_real_escape_string($_GET['oid'])))); 

    $re = ("SELECT * FROM (SELECT comment as comment, byuid as byuid, comuid as comuid, likes as likes, dislikes as dislikes, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_comments WHERE onuid = '$onuid' AND type = 'status' ORDER BY timestamp DESC LIMIT 2) mingle_comments ORDER BY timestamp ASC") ; //query 
    $result = mysql_query($re) or die (mysql_error()); 
    if(mysql_num_rows($result) >= 2) { 
     ?> 
     <div id="sa" style="background:#E0E0E0; padding:5px 5px 5px 5px;"> 
      <a href="#" style="font-family:Arial; font-size:12px; color:#3a3a3a; text-decoration:none;">View all comments...</a> 
     </div> 
     <?php 
    } 

    while($st = mysql_fetch_assoc($result)) { 
    $comment = $st['comment']; 
    $by = $st['byuid']; 
    $comuid = $st['comuid']; 
    $time = $st['timestamp']; 
    $l = $st['likes']; 
    $d = $st['dislikes']; 

    $bq = "SELECT * FROM users WHERE uid = '$by' LIMIT 1"; 
    $bqq = mysql_query($bq) or die (mysql_error()); 

    while($bqr = mysql_fetch_assoc($bqq)) { 
     $dp = $bqr['dp']; 
     $fbq = $bqr['fname']; 
     $sbq = $bqr['sname']; 
    } 
    ?> 

    <div id="commentbox" class="<?php echo $comuid; ?>" style="padding:5px 5px 5px 5px;"> 
     <div id="cbi" style=" display:inline; position:relative; "> 
      <img src="<?php if ($dp == null) { echo 'img/unknown_user.jpg'; } else { echo "pf/" . $by . "/" . $dp; } ?>" width="36px" style=" display:inline; position:relative;"/> 
     </div> 
     <div id="cbt" style="position:relative; margin-left:32px; margin-top:-35px;"> 
      <a href="profile.php?uid=<?php echo $uid; ?>" style="position:relative; font-size:13px; margin-left:10px; font-family:Arial; color:#3a3a3a; text-decoration:none;"><?php echo $fbq . " " . $sbq; ?></a> 
      <p class="<?php echo $comuid; ?>" style="position:relative; margin-left:5px;"><?php echo $comment; ?></p> 
     </div> 
     <div id="clb" style="background:#E0E0E0; padding-left:5px;"> 
      <a href="#">Like</a> <a href="#">Dislike</a> <a href="#" id="time"><?php echo time_since($time); ?></a> 
     </div> 
    </div> 

<?php 
} 
?> 

任何想法,爲什麼發生這種情況? :/另外,如果我按住回車鍵,它會多次提交,有什麼想法可以阻止它?

回答

0

另外,如果我按住回車鍵,它會多次提交它,任何 想法如何我可以防止這種情況?

使用按鍵事件而不是KEYDOWN

$('#c').load('ajax/querylc.php?oid=' + myClass); 

'#C' 選擇器選擇與ID 「C」 的第一個元素。因此,請嘗試生成此唯一ID(「#c1」,「#c2」等)。

+0

第二個解決方案工作,按鍵沒有。 – AviateX14

+0

嘗試「keyup」事件處理程序 – Ziumin