2015-12-28 29 views
1

我想讓用戶點擊網站上顯示的圖片,目前,圖片是從數據庫,並在一個循環。如果我設置一個ID,用戶只能點擊顯示的第一個圖片,無論如何爲$("#HELP").click(function(){允許我有一個代碼,但允許所有的圖片點擊?

<div id = 'HotelContentsmallpic'> 
<?php 
$cols=4;  // Here we define the number of columns 
    echo "<table>"; // The container table with $cols columns 
    do{ 
     echo "<tr>"; 
     for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if 
            // the records are less than $cols 
      $row=mysqli_fetch_array($result2); 
      if($row){ 
       $img = $row['IMG']; 
       $cat = $row['Category']; 
?> 
     <td> 
      <table> 
       <tr align="top"> 
        <td><img id = "HELP" src="images/Hotel/<?php echo $img ?>.jpg" class="img-responsive" /></td> 
        <td> 
        </td> 
        <td width="50">&nbsp;</td> <!-- Create gap between columns --> 
       </tr> 
      </table> 
     </td> 
<?php 
      } 
      else{ 
       echo "<td>&nbsp;</td>"; //If there are no more records at the end, add a blank column 
      } 
     ?> 
     <script> 
      $(document).ready(function(){       
     $("#HELP").click(function(){ 
     window.location='hotels.php?Category=<?php echo $cat ?>&Pic=<?php echo $img ?>'; 
       }); 
      }); 
     </script> 
     <?php 
     } 
    } while($row); 
    echo "</table>"; 
?> 
</div> 

回答

2

你需要改變兩兩件事:

在HTML中使用IMG tag這樣:

<img onclick="redirect_url('<?=$cat?>','<?=$img?>')" src="images/Hotel/<?=$img?>.jpg" class="img-responsive" /> 

而在JavaScript中使用這樣的:

<script type="text/javascript"> 
function redirect_url(cat,img) 
{ 
    window.location='hotels.php?Category='+cat+'&Pic='+img+''; 
} 
</script> 

在此方案中,沒有必要使用#HELP id在。

+0

YESSSS IT WORKS THANKS ^^您的生活救星 – Melvin

+0

提到不是,現在選擇了最好的答案和標記爲已接受,因爲這將有助於其他誰面臨這個問題。 @Melvin – devpro

0

您正在使用ID屬性,在這種情況下,您應該使用class屬性,因爲有多個「HELP」類型的元素。所以基本上#HELP到.HELP和id =「HELP」到class =「HELP」