【问题标题】:Manipulating only the first row of a foreach loop and manipulating all rows at the same time仅操作 foreach 循环的第一行并同时操作所有行
【发布时间】:2017-06-02 18:56:33
【问题描述】:

我正在尝试添加一个仅对管理员可见的删除按钮。但同时,第一行会为用户提供一个“我已阅读此信息”按钮。在此代码中,不能为所有行隐藏删除按钮。删除按钮仅在第一行隐藏。如果用户不是管理员,我希望删除按钮不会出现在任何地方。

我已经尝试过这段 javascript 代码,但它只影响我的 foreach 循环的第一行。我不知道该怎么做。你有什么想法吗?

 <?php     if(($_SESSION['username'] == 'Admin')) { 

    echo "<script>  $('#del_photo').hide(); </script> ";
    }




                foreach($select as $index => $rs) {


    ?>    

                <div class="post-id"   id="<?php echo $rs['id']; ?>" style="margin 300px; overflow: hidden; border-top-right-radius: 25px; border-top-left-radius: 25px;   display:block;
                position: relative; margin: 20px; border: 2px solid #222;">
                <br><br>
                <h2 class="blog-post-title"><?php echo $rs['title'];?></h2>
                <p class="blog-post-meta"> <?php echo $rs['date'];?> skrevs av <a href="#"> <?php echo $rs['author'];?></a></p>

                        <?php 
                            $i = 0;
                            $len = count($select);
                            foreach ($select as $item) {
                            if ($i == 0) {
                            echo  "<form action=\"#\" style=\" height:30px; background:transparent; border:none; color:transparent;\" method=\"get\"> <button name=\"readit\" id=\"readit\" 
                            style=\" height:30px; position:absolute; background:transparent; border:none; color:transparent; width:70px;  display: flex; top:-0.5%; left:0%; float:right; margin:5px 5px 0 0;\"  
                            class=\"del_photo\"> <span src=\"sett.png\" class=\"readit\" id=\"readit\" ></span><img src=\"sett.png\"  class=\"readit\"  border=\"0\" /></button> </form>";
                            } else if ($i == $len - 1) {
                            echo "// last";
                            }

                            $i++;
                            }   
                        ?>

                <br>
                <center><p style="margin-left:auto; margin-right: auto; display:block; height: 100%; max-width: 100%;  margin-bottom:0px;"><?php echo $rs['content'];?></p></center></br>
                <img style=" margin-left:auto; margin-right: auto; display:block; height: 100%; max-width: 100%;  margin-bottom:0px;" src='<?php echo $rs['image'];?>' width="400" height="300">            
                <button  id="del_photo" style=" background:transparent; border:none; width:350px; height: 250px; color:transparent;  display: inline-block; 
                float:right; margin:5px 5px 0 0; position: absolute; top: 0; right: 0;"  
                class="del_photo" onclick="deletepost('<?php echo $rs['id']; ?>')"> <span class="del_photo" id="del_photo" src="x.png"  style="  display: inline-block; 
                float:right; margin:5px 5px 0 0; position: absolute; top: 0; right: 0;" 
                id="" ></span><img  class="del_photo" id="del_photo" src="x.png"   style="  display: inline-block;  
                float:right; margin:5px 5px 0 0; position: absolute; top: 0; right: 0;"  class="del_photo"  border="0" /></button>
              </div>




    <?php
        }
    ?>

【问题讨论】:

  • 您的代码似乎不在循环中
  • 循环运行完美。我可以在浏览器上看到它。但我想通过隐藏按钮来影响所有行。我想为特定的会话呼叫隐藏它。
  • 在这里猛烈抨击因为我不完全确定,但将id="del_photo" 更改为class="del_photo" 之类的类名并将javascript #del_photo 更改为.del_photo(也从ID 到类) 元素 ID 是唯一的,因此只能出现一次。
  • 非常感谢。它有效。

标签: javascript php foreach


【解决方案1】:

问题是你告诉 jquery 隐藏带有id="del_photo" 的按钮

但是 id 是唯一的,因此在这种情况下不起作用,只会隐藏第一个, 所以你最好使用del_photo这个类

那就试试吧

echo "&lt;script&gt; $('.del_photo').hide(); &lt;/script&gt; ";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多