【问题标题】:Managing multiple comments boxes 'set' on single page - differentiate them and treat correctely在单个页面上管理多个评论框“设置” - 区分它们并正确对待
【发布时间】:2016-07-27 06:45:27
【问题描述】:

我在同一页面上有多个内容评论框,我无法识别它们以正确处理它们 - 例如,我尝试在哪里发布评论(来自哪个评论框)并不重要 -它总是被发布到页面上的第一个评论框,或者我在发布评论后与输入作斗争以清除但输入仅在同一个第一个评论框上清除。这是我所拥有的: (AJAX)

    $(function() {
     $(".comm").click(function() {
    var parentDiv = $(this).parent('.post_comment').next().children(":first");  
    var comment = $(this).parent('.post_comment').find('.comment').val();
     var name = document.getElementById("username").value;
     var idv = $(this).parent('.post_comment').find("#idv").val();
    var posthere =            $(this).parent().siblings('.wrap_comment').find('#wrap').attr('class');

   alert(idv);
  if(comment=='')
  {
    alert("Enter some text..");
     $("#content").focus();
      }
   else
        {
      $.ajax({
  type: "POST",
  url: "river_flow.php",
  data: 
  {  username: $("#username").val(),
     idv: $(this).parent('.post_comment').find(".idv").val(),
     comment: $(this).parent('.post_comment').find('.comment').val()},

    cache: false, 
   success: function(data){    
    $('#show').after(data);
    $(this).find('.comment').css('background-color','red');

   $("#content").focus();
    }})   
    return false;
        }})})

(HTML)

<div class='com'><button class='click_and_comment'>Give it a comment</button>

              <div class='post_comment'>
              <img class='end' src="img/end.png" alt='close'/>
                   <input type="hidden" id="username" value="<?php echo $_SESSION['username'] ?>" name='username'/>
                   <input type="hidden"class='idv' id="idv" value="<?php echo $row['idv']; ?>" name='idv'/>
                   <textarea type='text' class="input_comment comment"   placeholder='What do you think?' name='comment' id='comment' ></textarea>
                   <input type='submit' name='comment_submit' class="comment_submit comm" id='comm'/>

              </div>


                 <div class='wrap_comment' id="<?php echo $row['idv']; ?>" >


                 <div id="show" align="left" class="<?php echo $row['idv']; ?>" ></div>
                    <?php
                    $idvc = $row['idv'];

                    $query = " SELECT * FROM comments WHERE idvc= '$idvc' ORDER BY datec DESC;";

                    $result = mysqli_query($conn,$query);

                    while ($bow = mysqli_fetch_assoc($result)) {
       echo "<div class='each_comment' id='<?php echo $bow[idvc];?>' >".$bow['commentc']."</div> ";}

                    ?>
                 </div>

所以,在一个问题中形成我的问题:如何将用户 cmets 分别发布到他们“需要”发布的位置,以及如何仅清理“当前”或“此”输入 - 并不总是首先。再一次 - 一切都很完美,问题是评论框总是不止一个,我需要区分它们并相应地对待它们。这是我的猜测不起作用(矛盾的是我不会在这里),请解释一下,为什么它们不起作用,因为甚至很简单:

$(this).find('#comment').value = '';

不起作用!也许它没有“看到”这个属性。但为什么?!所以。以下是我的猜测:

 $(this).parent('.post_comment').siblings('.wrap_comment').find('#show').after(data);

(这是评论帖子,这是我对输入清除的猜测:)

$(this).parent('.post_comment').find('#comment').val('');

我发现了类似的东西:

$('#comment').removeAttr('value');

不起作用 - 我不是在谈论当这些东西不起作用时我得到的纯粹的死胡同:

$(this).find('.comment').css('background-color','red');

哎呀。所以,我希望有人可以为我清除这一切。我是新手说实话。谢谢!

【问题讨论】:

  • 好的。请给我链接好吗?
  • 谢谢你让我看看我能做些什么来提供帮助
  • 你去!请帮我解决这个问题
  • 我无法找到您的确切问题所在,是您的 jquery 代码中的某一行吗?

标签: javascript jquery html comments this


【解决方案1】:

使用以下脚本并用此代码替换您的相关脚本。我没有在 class="com" 的 div 中找到多个评论框,所以我认为它只出现在这个 div 中,而不是其他任何地方:

$(function() {
        $(".com").click(function() {

            //##############################$(this) here refers to div with class="com" as this is the element on which click event has happened#####################
            var myThis = $(this);
            //var parentDiv = $(this).parent('.post_comment').next().children(":first");  

            //since div with class="post_comment" is inside div with class="com" so it is child to that div and not parent
            var parentDiv = $(this).find('.post_comment').next().children(":first"); 

            //var comment = $(this).parent('.post_comment').find('.comment').val();
            var comment = $(this).find('.post_comment .comment').val();//find element with class="comment" whose parent is div with class="post_comment"

            var name = document.getElementById("username").value;

            //var idv = $(this).parent('.post_comment').find("#idv").val();
            var idv = $(this).find(".post_comment #idv").val();//find element with id="idv" whose parent is div with class="post_comment"

            //####Not sure what is the use of below statement
            var posthere = $(this).find('.wrap_comment #wrap').prop('class');

            alert(idv);
            if (comment == '')
            {
                alert("Enter some text..");
                $("#content").focus();
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "river_flow.php",
                    data: {username: $("#username").val(),
                                idv: idv,
                                comment: comment 
                    },
                    cache: false,
                    success: function (data) {
                        $('#show').after(data);
                        //### $(this) here will not point to your div with class="com" as it is in different context inside your ajax success function
                        //##$(this).find('.comment').css('background-color', 'red');
                        myThis.find('.comment').css('background-color', 'red');

                        $("#content").focus();
                    }
                });
                return false;
            }
        });
    });

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 2013-02-26
    • 2013-08-14
    • 1970-01-01
    • 2013-01-13
    • 2012-07-14
    • 2020-05-12
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多