2017-04-22 63 views
0

我創建了一個由php循環生成的php。基本上從我的數據庫加載所有帖子。 在視圖使用的html模板上,我添加了一些jquery來顯示一個隱藏的元素(以更新對加載的帖子的評論),並且工作正常,但是當我在我的jquery事件中選擇一個類時,當我單擊它時顯示所有隱藏具有相同類別的元素...這是正常的,但我試圖動態添加一個數字,以便當點擊時我可以指向該屬性... 這是我的觀點 - 我已經放大了元素我:php查看jQuery的事件

<?php ob_start();?> 
<?php $titlecomment = 'Comments :'; ?> 
<?php foreach ($com as $c): ?> 
    <?php $idcomment = $c->getIdComment()?> 
    <?php $id = (int)$_GET['id'] ?> 
    <div class="container-fluid bg-1 text-center"> 
     <form method ="POST" action="<?="../Controler/commentDelOrUpdate.php?idcomment=".$idcomment."&id=".$id;?>"> 
      <div class="row"> 
       <div class="col-sm-offset-6"> 
        <div class="col-lg-2 col-md-3 col-sm-2"><?= $c->getDate()?></div> 
        <div class="col-lg-2 col-md-4 col-sm-2"><?= $c->getName()?></div> 
        <div class="col-md-offset-1 col-lg-3 col-md-4 col-sm-3"><?= $c->getComment()?></div> 
        <div> 
         <div type="button" class="col-sm-0.5 btn btn-primary editField" name="edit">Edit</div> 
         <button type="submit" class="col-sm-0.5 btn btn-primary" name="delete">Delete</button> 
        </div> 
        <br> 
        **<textarea rows="2" class="col-lg-offset-3 col-lg-3 col-md-4 col-sm-3 hiddenField" style="display: none; color: black;" name="newcomment"></textarea> 
        <button type="submit" class="col-lg-2 col-md-4 col-sm-1 btn btn-primary hiddenField" style="display: none" name="update">Update</button>** 
       </div> 
      </div> 
     </form> 
    </div> 
<?php endforeach;?> 
<?php $comment = ob_get_clean();?> 

And here is my jQuery: 

<script> 
     $(function() { 
      //add number to class hiddenField 
      //increment that class 
      //event on click to the selected class 

      $(".editField").on('click',function() { 
       $('.hiddenField').toggle(); 
      }); 
     }); 
    </script> 

回答

0

而是選擇所有.hiddenField元素,你可以使用DOM導航功能,專門找到元件附近的那些被點擊。基本上導航到最接近的通用父元素,然後找到該父元素中的.hiddenField元素。類似這樣的:

$(".editField").on('click',function() { 
    $(this).closest('div.row').find('.hiddenField').toggle(); 
}); 
+0

完美的作品!謝謝大衛! – LMD4U