简体   繁体   English

你能指导我一个问题吗,我每次按下按钮时都会尝试更改颜色

[英]Can you guide me with a question, I am trying to change the color every time I press a button

My problem is that I call my function within my for, but only a part changes color and not above each record, the buttons work but the color of the corresponding row does not change, it had occurred to me to send a value that could be the pk of my table and this one that is changing in the for, if you know any way to achieve that I would really appreciate it我的问题是我在我的 for 中调用了我的 function,但只有一部分改变了颜色,而不是每条记录上方,按钮工作但相应行的颜色没有改变,我想到发送一个可能是的值我的桌子的 pk 和这个正在改变的桌子,如果你知道任何方法来实现它,我会非常感激

<!doctype html>


<html lang="es">
<head>

<body>


          <?php 
               
if (isset($_POST['submit']) && !hash_equals($_SESSION['csrf'], $_POST['csrf'])) {
  die();
}

$error = false;
$config = include 'ClientesBeta/conexion/config.php';

try {
  $dsn = 'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'];
  $conexion = new PDO($dsn, $config['db']['user'], $config['db']['pass'], $config['db']['options']);

  if (isset($_POST['marca'])) {
    $consultaSQL = "SELECT * FROM zonas WHERE idZonas <> 1 and nombreZona LIKE '%" . $_POST['marca'] . "%'";
  } else {
    $consultaSQL = "SELECT * FROM zonas where idZonas <> 1";
  }

  $sentencia = $conexion->prepare($consultaSQL);
  $sentencia->execute();

  $alumnos = $sentencia->fetchAll();

} catch(PDOException $error) {
  $error= $error->getMessage();
}
            ?>
            <table class="table table-bordered table-striped" style="margin-top:20px;">
                <thead>
                    <th>ID</th>
                    <th>Zona</th>
                    
                    <th>Acción</th>
                </thead>
                <tbody>
                    <?php
          if ($alumnos && $sentencia) {
              
            foreach ($alumnos as $row) {
                
              ?>
              
  <tr>
   
    <td  id="prueba2"></td>   
    <td id="prueba"></td>
     <td id="prueba1"></td>
      <td >
<button  type="button" onclick="cambiarColor2();">Inicio</button>
<button type="button" onclick="cambiarColor();">Proceso</button>
<button type="button" onclick="cambiarColor1();">Fin</button>
</td>

<script>
          function cambiarColor(){
    document.getElementById('prueba').style.backgroundColor = "blue";
  }
    </script>
    <script>
          function cambiarColor1(){
    document.getElementById('prueba1').style.backgroundColor = "red";
  }
    </script>
    <script>
          function cambiarColor2(){
    document.getElementById('prueba2').style.backgroundColor = "green";
  }
    </script>
  </tr>

              <tr>
                                    <td><?php echo $row['idZonas']; ?></td>
                                    <td><?php echo $row['nombreZona']; ?></td>
                                    
                                    
                                </tr>
                                <?php 
                            }
                        }
                        

                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>

It is difficult to understand your question.很难理解你的问题。 But I believe you are looking for the below answer.但我相信您正在寻找以下答案。 Let me know if it helps you.让我知道它是否对您有帮助。

I think you have placed the ID in the wrong tags, though.不过,我认为您将 ID 放置在错误的标签中。 Changed the id to where you want to change the colour.将 id 更改为要更改颜色的位置。 I haven't tested the code also.我也没有测试过代码。

<?php 
if ($alumnos && $sentencia) {
    foreach ($alumnos as $key => $row) {?>
        <tr>
            <td id="prueba2_<?php echo $key; ?>"></td>   
            <td id="prueba0_<?php echo $key; ?>"></td>
            <td id="prueba1_<?php echo $key; ?>"></td>
            <td>
                <button  type="button" onclick="cambiarColor(<?php echo $key; ?>, 2, 'green');">Inicio</button>
                <button type="button" onclick="cambiarColor(<?php echo $key; ?>, 0, 'blue');">Proceso</button>
                <button type="button" onclick="cambiarColor(<?php echo $key; ?>, 1, 'red');">Fin</button>
            </td>
        </tr>
        <tr>
            <td><?php echo $row['idZonas']; ?></td>
            <td><?php echo $row['nombreZona']; ?></td>
            <td>&nbsp;</td>
        </tr>
<?php 
    }
}
?>
<script>
    function cambiarColor(id, eleid color){
        document.getElementById('prueba'+eleid+'_'+id).style.backgroundColor = color;
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任何人都可以通过我尝试使用 js 或 python 组合在一起的可视化来指导我吗? - Can anyone guide me with a visualization I am trying to put together using js or python? 当您使用Jquery按下按钮时,我试图使页面滚动速度变慢吗? - I am trying to make my page scroll slower when you press a button using Jquery? 我正在尝试制作一个程序来让我的课程剩余时间,但更改后的时间表不起作用,你能告诉我为什么吗? - I am trying to make a program to get the time left in my classes but the altered schedule isn't working can you tell me why? 我正在尝试做这个问题,但是 state 给我带来了问题 - I am trying to do this question but, state is giving me problems 当我按下按钮时,如何更改html标签的颜色? - How can I change color in tag of html when I press a button? 当我按下后退按钮时,我试图后退 - am trying to back when i press the back button 每次按下按钮时如何打开 GIF - How to open a GIF every time I press a button 当我使用鼠标 hover 使用 CSS 时,我试图让按钮中的文本更改颜色 - I am trying to make the text in a button change color when I hover over in with the mouse using CSS a内的总和值<input>每次我按下一个按钮 - Sum value within a <input> every time I press a button 每次按下下一个问题按钮时,我都想将其更改为数组中写入的内容 - I want to change this to what is written in the array every time a next question button is pushed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM