2017-06-22 75 views
0
<? foreach(.........){      

<select id = "RelationshipSelect" name = 'RelationshipSelect' disabled> 
           //some codes implement               
         </select>`enter code here` 

         <input type = 'text' value = "<? echo ... ?>" id = "affectedSystems_0" name = 'affectedSystems_0' readonly> 

            <input type = "button" id = 'findAffectedSystems_0' value = 'Find' disabled> 
             <input type = "button" id = 'removeAffectedSystems_0' value = 'Remove' disabled> 


     <?}?> 

as showing in the photo, CI Relationship Field data are retrieve using foreach loop.多個按鈕具有內部foreach循環,但只有第一個按鈕可以單擊。如何使所有的按鈕可以點擊?

在JavaScript文件,我稱之爲「findAffectedSystems_0」爲找到按鈕的ID,但它僅適用於「查找」按鈕的第一行。我應該怎麼做才能讓所有的按鈕都可以點擊。

document.getElementById("findAffectedSystems_0").addEventListener("click", function(){ 
       //some codes work 

}); 
+2

使按鈕的ID獨特。 – Twinfriends

+2

查找和刪除按鈕不能全部具有相同的「ID」。你需要使它像'findAffectedSystems_0','findAffectedSystems_1'那樣動態化。 –

+0

謝謝@JigarShah。但我怎麼可以在循環內獲取按鈕的動態ID。請幫助我,我在這個新手 –

回答

0

你應該做這樣的

foreach ($variable as $key => $value) { // $variable should be array 
     ?> 
     <input type="button" class="common-class" name="findAffectedSystems_<?php echo $key ?>" id="findAffectedSystems_<?php echo $key ?>"> 
     <input type="button" class="common-class" name="removeAffectedSystems_<?php echo $key ?>" id="removeAffectedSystems_<?php echo $key ?>"> 
     <?php 
    } 

,如果你需要在每個BTN單獨的行動,然後用id屬性去,否則需要採取共同行動,你應該使用類屬性

+0

謝謝,它工作正常:) –

+0

高興地幫助你:) –

+0

@P.Som請接受一個答案,如果你發現這個有用的,所以問題可以關閉 –

相關問題