doucaigai7176 2017-12-19 16:31
浏览 45
已采纳

对数据库中的数据进行操作

I have page with data from database:

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$base = 'zar';
try {

$db = new PDO('mysql:host='.$host.';dbname='.$base.';charset=utf8', $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

$name = 'name';
$pin = 'pin';
$ip = 'ip';
$id = 'id';

$statement = $db->query('SELECT id, ip, name, pin FROM devic');

foreach($statement as $wiersz)
{

?>
<div id = "<?php print($wiersz['id'])?>"
<img class= "obraz"  src="css/bulb_off.png" alt="...">
 <div class="relayBlock"><span class="relayTitle"><?php print($wiersz['name']) ?> </span>
    <button class="btn btn-block btn-lg btn-primary " value="on">Wł</button>
    <button class="btn btn-block btn-lg btn-danger " value="off">Wył</button>
</div>
</div>
<?php
}   
$statement->closeCursor();

} catch(PDOException $err) {
    exit('error: '.$err->getMessage());
}
?>

JS:

<script type="text/javascript">
        $(document).ready(function () {
            $('.btn').click(function() {
                var val = $(this).val();            
                $.ajax({
                    url: "try.php",
                    type: "POST",
                    data: {'myVar': val},                         
success : function(data) {
        if (data == 1) {

            $('.obraz').attr({src : "css/bulb_on.png"})
        }
        else{
            $('.obraz').attr({src : "css/bulb_off.png"})
    }
}
      });
            });
        });
    </script>

Now, if I click any button, the pictures for all the rows in the database change. I want to do so that after clicking a button for a specific row from the database, the picture changed only for that line. I know that this is because every picture has the same class for the picture, but can I do it in a different way?

  • 写回答

2条回答 默认 最新

  • duanshan1511 2017-12-19 16:40
    关注
    var idParent = getvalueId from the div parent
    if (data == 1) {
         $('#' + idParent +' .obraz').attr({src : "css/bulb_on.png"})
    } else{
         $('#' + idParent +' .obraz').attr({src : "css/bulb_off.png"})
    }
    

    jquery, selector for class within id

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

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)