2015-05-26 34 views
2

//我的問題是當我的checkit()函數被調用時,它只在數組中的第一項旁邊顯示check.png圖像。javascript onclick屬性如何用於循環數組的更多輸出?

<?php 
require 'connectit.php'; 

if($result = $db->query("SELECT * FROM people ORDER BY id")) { 
    if($count = $result->num_rows) { 

     echo "<h3>He's got $count bills to pay.</h3>"; 
     echo "<p>"; 
     while($row = $result->fetch_object()) { 
      echo "<div id=\"check\" onClick=\"checkit();\"><img id=\"img\" src=\"\" /></div>"; 
      $id = $row->id; 

      echo $row->bill_name, ': $', $row->bill_cost, 
      ' ',"<a href=\"delete_id.php?id=" . $id . "\">X</a><br/><br/>"; 
      echo "</p>";  
     }  
    } 
} 
?> 
//javascript onclick function 
<script type="text/javascript"> 
function checkit(){ 
document.getElementById("img").src = "check.png"; 
//needed to put a line through the text, to mark through it but can't figure that out either. I tried this. 
document.getElementById("p").style.texttransform = "underline"; //didn't work for me. 
} 
</script> 

回答

1

試試這個代碼:

var list = document.getElementsByTagName("img"); 

for(var i=0;i<list.length;i++) { 
    list[i].src = "check.png"; 
} 

OBS:我沒有測試它。

更新:

var list = document.getElementsByTagName("img"); 

for(var i=0;i<list.length;i++) { 
    if(list[i].id == 'some_id') { 
     list[i].src = "check.png"; 
    } 
} 
+0

謝謝,是的,他們立即檢查他們是否有辦法讓他們檢查個人?希望我可以用javascript打開和關閉它。 –

+0

@DrewPeer你也可以檢查List Index的id是否等於你想改變的' id'。檢查我的更新。 – alexandreferris

+0

問題是他們都有相同的標識,他們仍然都檢查。我希望我可以向你發送文件。它也是活的。 [email protected]如果你有興趣看看它的樣子。 –

1

HTML中的所有「id」屬性都必須是唯一的。

爲了提高效率,document.getElementById()停在第一個包含您要查找的id的元素上。

+0

更有意義。謝謝 –