2016-10-19 47 views
0

如何使用PHP計數器通過javaScript顯示隱藏文本字段。我有這樣的代碼在HTML成立的PHP:單擊按鈕時顯示隱藏對象(PHP with javaScript)

<?php if(isset($prior)): foreach($prior as $row): ?> 
    <tr> 
     <td> <?php echo $row->priority?> </td> 
     <td> <?php echo $row->priority_time?> </td> 
     <td> 
       <button type="button" class="btn btn-info btn-sm" onclick=""> 
       <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 
       </button> 
       <input type="text" id="" name="<?php echo $row->priority_id?>" hidden/> 
     </td> 
    </tr> 
    <?php endforeach;?> 
    <?php else:?> 
     <p>Request Service not available</p> 
    <?php endif;?> 

回答

0

先給你隱藏輸入自己的ID,然後在要刪除隱藏類函數內部:

var myButtonClasses = document.getElementById("theGivenId").classList; myButtonClasses.remove("hidden");

來源:classlist

0

在你的PHP循環:

<button type="button" class="btn btn-info btn-sm" onclick="showField(<?php echo $row->priority_id; ?>)"> 
    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 
</button> 

添加JS功能頁面:作爲隱藏輸入

function showField(name) { 
    var el = document.querySelector('input[name="' + name + '"]'); 
    el.setAttribute('hidden', false); 
} 
+0

如何在javaScript中添加php代碼? – Char

+0

您並未向JS添加PHP代碼。您正在向該頁面添加一個名爲'showField'的JS函數,並使用PHP將'name'參數添加到在'button'的'onclick'內調用的函數中。 – Enijar

0

使用CSS,讓按鈕來調用javascript函數中,你可以與 的document.getElementById一起做你的其他任務(「theGivenId」) .style.visibility =「visible」;