如何使用 jquery closest 函数

how to use jquery closest function

我制作了一个网页,其中我将图像上传到数据库,然后从数据库中再次显示它们。在上传图像时,我在文本区域中向其添加评论,并且在再次显示时显示评论并且也是可编辑的,现在我添加了一个带有显示 none 的添加评论文本区域,并希望在单击具有 jquery 显示功能的按钮时显示它。
我能够显示(使用 jquery ) textarea(有 display:none 属性)点击 'Add Comment' 按钮但问题是它总是打开最后上传图片的 textarea
我读过关于使用最近的 属性 的 jquery 但它不适用于我的代码,我可能遗漏了一些我不知道的东西
这是我需要的 css 如果你尝试运行程序..

    #commen{
        display:none;
    }
    #sq {
    background-image:url("wood.jpg");
        margin-top:1%;
       margin-left:30%;
       margin-right:20%;
       height: 70%;
        width:40%;
        float:left;}

我在上传时显示图片和评论输入的代码,有一个添加按钮

        function displayimage()
    {
    <div id="sq">
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "databaseimage";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = mysqli_query($conn,"SELECT * FROM images order by         image_id         DESC");

    while ($image = mysqli_fetch_assoc($sql)) 
    {
    echo '  <img src="data:image/jpeg;base64,'.base64_encode(         $image['url'] ).'" width="100%" height="100%">';
    $id=$image['image_id'];
    $sqlit = mysqli_query($conn,"SELECT * FROM images_comments         where            comment_id='$id'");
         $image1 = mysqli_fetch_assoc($sqlit);  
         $text = $image1['text'];
    echo "<form method='post' action='page.php'>
         <textarea name='comment'          onblur=style.backgroundColor='darkgray'                 onclick=style.backgroundColor='white'>$text</textarea>
         <input type='hidden' name='imageID' value='$id'/>
         <input type='submit' name='submitComment' value='Submit'></form>";

         echo "<div id='commen'>
         <form method='POST' action='comm.php'>                           
         <textarea name='commentx'></textarea>            
         <input type='submit' name='Adcom' value='Add'>
         </form>   
         </div><a class='comment'>Add Comment</a>";
         echo nl2br("\n");
    }

    ?>
    </div>

我的jquery函数

<script>
   $(document).ready(function(e) {

     $('.comment').click(function(){    
     $('#commen').show();
     });
   });
   </script>

我只是不知道如何使用 closest 函数。
只需要知道如何在这里正确使用 closest 函数
感谢任何帮助,谢谢...

我也试过这段代码来显示..

$('.comment').click(function(){    
$(this).prev().show();
 });

首先,您可能不应该在同一页面上有多个具有相同 id 的元素。您可能想要的 JavaScript 是

$('.comment').click(function(){    
    $(this).prev().show();
});