![](/img/trans.png)
[英]Attaching data to a button in a cell from a row in a table that is dynamically generated
[英]Execute a Javascript function from a button in a table row cell which was generated by PHP echo
我想從表行單元格調用Javascript函數。 我也需要傳遞該行的id。
在一個單元格中,我使用一個href(它彈出我的編輯對話框),但不傳遞Id(BrId)。 下一個,理想情況下是一個調用Javascript函數的按鈕(雖然我已經看到了將$(document).ready(function() {.....etc}
中的click事件函數關聯起來的代碼/函數)但不確定如果這將獲取所需的Id(BrId),它是數據庫表的主鍵。
代碼是:
foreach ($myrows as $row) {
echo "<tr>";
echo '<td style="border:none;">' .$row->BrId. '</td>';
echo '......'
echo '......'
echo '<td style="border:none;"><a href="#dialog" name="modal">Edit this branch </td>';
echo '<td style="border:none;"><button onclick="EditBranch (1)"></td>';
}
理想情況下,該函數還會顯示我的彈出div(id = dialog),因為"a href="#dialog" name="modal"
。
如果這有幫助,這是腳本的一部分:
$(document).ready(function() {
//select all the a tags with name equal to modal
$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href'); //gets me my div id
//other code for transition effects and positioning of my div....
}
您可以通過自定義html數據屬性傳遞行ID:
echo '<a href="#dialog" name="modal" data-id="' . $row->BrId . '"> Edit this branch </a>';
然后,您只需檢索它:
var id = $(this).attr('data-id');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.