通过ajax按键提交textarea并动态加载内容

时间:2012-06-25 08:44:02

标签: php jquery mysql ajax

是的,我解决了我之前无法在返回键上提交textarea但是提出了一个新问题的问题。它通过ajax提交注释,然后在第一个状态下加载它。无论我尝试将哪个帖子添加到评论中,它都会将评论附加到页面上的第一个状态div,而不是正在执行操作的那个。但是,如果我刷新页面,注释会显示在正确的状态,所以我知道正在提交正确的ID,它只是被附加在错误的位置,但我看不出原因。

我的提交代码:

$(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
}
?>

为什么会发生这种情况? :/另外,如果我按下返回键,它会多次提交,任何想法我怎么能阻止它?

1 个答案:

答案 0 :(得分:0)

  

另外,如果我按住返回键,它会多次提交,任何   想法如何防止这种情况?

使用keypress事件代替keydown

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

'#c'选择器选择id为“c”的第一个元素。因此,请尝试将此id生成为唯一ID(“#c1”,“#c2”等)。