仅在while循环中附加到当前的div元素 [英] Append to current div element in a while loop only

查看:92
本文介绍了仅在while循环中附加到当前的div元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有以下代码结构。此代码结构加载一个页面,其中包含不同的帖子主题和内容。对于每个加载的帖子,用户可以留言。看看下面的代码:

 <?php 
$ getposts =SELECT * FROM posts;
if($ row = $ result> fetch_assoc()){

$ id = $ row1 ['id'];
$ subject = $ row1 ['sub'];
$ content = $ row1 ['body'];
$ comments = $ row1 ['comments'];

echo
< div class ='content_wrap'>
< div class ='subject'> $ subject< / div>
< div class ='content'> $ content< / div>
< div class ='comment'> $ comments< / div>
< / div>
;
?>
< form id ='reply'autocomplete =off>
< input type =hiddenvalue =<?php echo $ id;?> name =idid =pid/>
< input type =textname =commentid =commentplaceholder =在这里输入一个通讯...>< / input>
< / form>

<?php
}
}
?>

< script type =text / javascript>
$('form')。submit(function(){
var comment = $(this).find('input [name = comment]')。val();
var id = $(this).find('input [name = id]')。val();
$ .post(comments_ins.php,{pub_id:id,comment:comment});
$(this).parent()。children('。content_wrap')。append(< div
class ='comment'+ comment +< / div);
var comment = $(this).find('input [name = comment]')。val('');
return false;
});
< / script>

以上所有代码都在一页中显示...并且该文件是一个php文件类型。

请注意,根据while循环,会在页面上为每个加载的帖子生成一个表单。
上面的代码约有90%正确。但有一件事我似乎并不正确。
我希望当用户离开评论时,该评论仅显示在该帖子的页面上....将页面上已加载的帖子锁定。
因此,如果用户输入第一条评论,则该评论是可见的,并且如果用户对该同一帖子键入另一条评论,则新评论将落入之前发布的评论....并且可见。因此,每当用户输入评论时,它都会附加到div元素。



如何执行此操作?



如果您看过上面的JavaScript,我尝试了一下但它不适合我。

指导将不胜感激。
Thanks

解决方案

尝试将ID附加到您的div,以便您更轻松地找到它们。
$ b

  $ id = $ row1 ['id']; 
$ subject = $ row1 ['sub'];
$ content = $ row1 ['body'];
$ comments = $ row1 ['comments'];

echo
< div class ='content_wrap'>
< div class ='subject'id = subject_ $ id> $ subject< / div>
< div class ='content'id = content_ $ id> $ content< / div>

< / div>
;

这应该让你追加到你想要的div。


Good day everyone,

I have the following code structure. This code structure loads a page with different post subject and content on it. For each loaded post, a user can leave a comment. Have a look at the following code:

 <?php
      $getposts = "SELECT * FROM posts";
      if ($result = $con->query($getposts)) {
         while ($row1 = $result->fetch_assoc()) {

         $id = $row1['id'];
         $subject = $row1['sub'];
         $content = $row1['body'];
         $comments = $row1['comments'];

         echo"
           <div class='content_wrap'>
             <div class='subject'>$subject</div>
             <div class='content'>$content</div>
             <div class='comment'>$comments</div>
           </div>
         ";
    ?>
   <form id='reply' autocomplete="off">
     <input type="hidden" value="<?php echo $id;?>" name="id" id="pid"/>
     <input type="text" name="comment" id="comment" placeholder="Type a commmment here..."></input> 
   </form>

  <?php
   }
  }
 ?>

 <script type="text/javascript">
  $('form').submit(function () {
          var comment = $(this).find('input[name=comment]').val();
          var id = $(this).find('input[name=id]').val();
          $.post("comments_ins.php", { pub_id: id, comment: comment});                              
          $(this).parent().children('.content_wrap').append("<div 
          class='comment'" + comment + "</div");                                   
          var comment = $(this).find('input[name=comment]').val('');
          return false;
        });
    </script>      

All of the above codes I currently have in one page...and the file is a php file type.

Notice that there is a form generated for each loaded post on the page according to the while loop. The code above works about 90 percent correct. But one thing I don't seem to get right. I want that when a user leaves a comment, that the comment displays on the page for that post only....nut all of the loaded posts on the page. So, if a user types a first comment, it is visible, and, if the user types another comment on to that same post, the new comment falls under the previous comment posted....and is visible ....so that it appends to a div element each time a user types a comment.

How do I go about accomplishing this?

If you look at my javascript above, I made an attempt but it did not work for me.

Guidance would be appreciated. Thanks

解决方案

try appending an ID to your divs so you can locate them more easily

         $id = $row1['id'];
         $subject = $row1['sub'];
         $content = $row1['body'];
         $comments = $row1['comments'];

         echo"
           <div class='content_wrap'>
             <div class='subject' id=subject_$id>$subject</div>
             <div class='content' id=content_$id>$content</div>
             <div class='comment' id=comment_$id>$comments</div>
           </div>
         ";

This should let you append to only the div you want it on.

这篇关于仅在while循环中附加到当前的div元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆