javascript onclick更改图像无法正常工作

时间:2015-10-23 07:05:08

标签: javascript onclick

我有两个数据,每个数据有4个图像,我想逐个显示,我想通过点击按钮使用onclick javascript交换图像。但它并不适用于所有数据,只是工作到数据结束。这是我的代码;

<?php
include "koneksi.php"; // connect to database

?>
<html>
<head>
<title></title>
<style>
    img{
        height:100px;
        width:100px;
    }
</style>

</head>
<body>
<table>
<?php
$result=mysqli_query($koneksi, "SELECT * FROM tbbarang where idbarang='43' or idbarang='44'");
$i= 1;
while ($row=mysqli_fetch_array($result))
{ ?>
   <tr>
   <!--image 1--> <td><img id="img<?php echo $i?>" src=" <?php echo $row['gmbr1'] ?>"></td> <!-- image that i want to swap with folowing images 1, image 2, image 3, image 4-->
   <!--image 2--> <td><img src="<?php echo $row['gmbr2'] ?>"></td>
   <!--image 3--> <td><img src="<?php echo $row['gmbr3'] ?>"></td>
   <!--image 4--> <td><img src="<?php echo $row['gmbr4'] ?>"></td>
   <script>
    function changeImage(id) {
        var image = document.getElementById("img"+id);
        if (image.src.match("<?php echo $row['gmbr1']?>")) {
            image.src = "<?php echo $row['gmbr2']?>";
        } else if (image.src.match("<?php echo $row['gmbr2']?>")) {
            image.src = "<?php echo $row['gmbr3']?>";
        } else if (image.src.match("<?php echo $row['gmbr3']?>")) {
            image.src = "<?php echo $row['gmbr4']?>";
        } else if (image.src.match("<?php echo $row['gmbr4']?>")) {
            image.src = "<?php echo $row['gmbr1']?>";
        }
    }   
    </script>
   <td><button id="ubah" onClick="changeImage(<?php echo $i?>)">Click To Change</button></td>
   </tr>
   <?php
   $i=$i+1;
}
?>

</table> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的文件可能不包含任何更改图像功能更改您的代码

<?php
include "koneksi.php"; // connect to database

?>
<html>
<head>
<title></title>
<style>
    img{
        height:100px;
        width:100px;
    }
</style>

    <script>
        function changeImage(id, gmbr1, gmbr2, gmbr3, gmbr4) {
            var image = document.getElementById("img"+id);
            if (image.src == gmbr1) {
                image.src = gmbr2;
            } else if (image.src == gmbr2) {
                image.src = gmbr3;
            } else if (image.src == gmbr3) {
                image.src = gmbr4;
            } else if (image.src == gmbr4) {
                image.src = gmbr1;
            }
        }   
    </script>

</head>
<body>
<table>
<?php
$result=mysqli_query($koneksi, "SELECT * FROM tbbarang where idbarang='43' or idbarang='44'");
$i= 1;
while ($row=mysqli_fetch_array($result))
{ 
    $gmbr1 = $row['gmbr1'];
    $gmbr2 = $row['gmbr2'];
    $gmbr3 = $row['gmbr3'];
    $gmbr4 = $row['gmbr4'];

?>



   <tr>
   <!--image 1--> <td><img id="img<?php echo $i?>" src=" <?php echo $gmbr1 ?>"></td> <!-- image that i want to swap with folowing images 1, image 2, image 3, image 4-->
   <!--image 2--> <td><img src="<?php echo $gmbr2 ?>"></td>
   <!--image 3--> <td><img src="<?php echo $gmbr3 ?>"></td>
   <!--image 4--> <td><img src="<?php echo $gmbr4 ?>"></td>

   <td><button id="ubah" onClick="changeImage("<?php echo $i?>", "<?php echo $gmbr1 ?>", "<?php echo $gmbr2 ?>", "<?php echo $gmbr3 ?>", "<?php echo $gmbr4 ?>")">Click To Change</button></td>
   </tr>
   <?php
   $i=$i+1;
}
?>

</table> 
</body>
</html>