douaipi3965 2017-10-24 09:24
浏览 64
已采纳

PHP while循环和使用javascript / Jquery与循环内的不同元素相关联

As far as I got with my application, I failed to use javascript when I am using PHP while loops. I can`t find a simple answer, or I am stupid enough to don't see the answer.

We all know that while loops returns many results and takes apart each new element by recognising it after his own identification element ( ID from database, for example).

I have the code:

<?php while ($row = mysqli_fetch_array($query)) 
      {
         echo "<td><input type='hidden' value='$row[0]' /></td>"; // here I take individually, each element ID from DB.
         echo "<td><span class='obs_1'>" . $row[4] . " <a href='javascript: show_comment()'>(show obs_2 and hide obs_1)</a></span></td>";
         echo "<td><span class='obs_2' style='display: none;'>" . $row[7] . " <a href='javascript: hide_comment()'>(show obs_1 and hide obs_2)</a></span></td>";
      }

Javascript:

<script>
function show_comment()
     {
         $('.obs_1').hide();
         $('.obs_2').show();
     }

function hide_comment()
     {
         $('.obs_2').hide();
         $('.obs_1').show();
     }
</script>

When you click on show_comment, obs_1 span must disappear and obs_2 span must be shown for each new element ID. The opposite thing for obs_2.

I need to assign that ID (value $row[0]) to the classes obs_1 and obs_2 and I can't manage to do that, in order to show the right obs_2 and hide obs_1 for each new element ID.

I have trouble finding answers when it comes to use javascript in PHP while loops.

  • 写回答

4条回答 默认 最新

  • dongshangan2074 2017-10-24 10:00
    关注

    I you use jQuery, you could use [data-id]. For instance in your case:

    <?php while ($row = mysqli_fetch_array($query)) 
          {
             echo "<td><input type='hidden' value='$row[0]' /></td>"; // here I take individually, each element ID from DB.
             echo "<td><span class='obs_1' data-id='".$row[0]."'>" . $row[4] . " <a href='javascript: show_comment()'>(show obs_2 and hide obs_1)</a></span></td>";
             echo "<td><span class='obs_2' data-id='".$row[0]."' style='display: none;'>" . $row[7] . " <a href='javascript: hide_comment()'>(show obs_1 and hide obs_2)</a></span></td>";
          }
    

    So after that in jQuery you can find the right span to hide or show:

        jQuery(".obs_1").on('click' ,function(event){
    var id = $(this).attr("data-id");
    $('.obs_1[data-id="'+id+'"]').hide();
    $('.obs_2[data-id="'+id+'"]').show();
    });
    

    This way anytime you click on a .obs_2 class you will have the right behavior only for the particular data-id elements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机