我有一個包含兩個表的頁面:客戶和車站。一個 sql 查詢用數據填充這些表。我在表格的每一行上定義了一個按鈕來打開一個模態編輯表單。對于客戶:<table id = "admin_table_customers" class="table rtl table-sm table-dark table-hover"> <tr> <th>customer_id</th> <th>name</th> <th> </th> <th> </th> </tr> <?php foreach($query_customers as $customer) { ?> <tr> <td><?php echo h($customer['CUSTOMER_ID']) ?></td> <td><?php echo h($customer['CUSTOMER_NAME']) ?></td> <td><button type="button" id="button_edit_customer" class="btn btn-primary feed-id" data-toggle="modal" data-target="#edit_customer" data-id="<?php echo h(u($customer['CUSTOMER_ID'])) ?>">edit</button></td> <td><button type="button" id="button_delete_customer" class="btn btn-primary feed-id" data-toggle="modal" data-target="#delete_customer" data-id="<?php echo h(u($customer['CUSTOMER_ID'])) ?>">delete</button></td> </tr> <?php } ?></table>當我在 chrome 中檢查源時,一切(id 和 data-id)看起來都很好。我使用這個腳本來檢查是否將正確的值傳遞給模態表單。$(document).ready(function () { $("#button_edit_station").click(function(evt){ alert($(this).attr("id")); alert($(this).data('id')); }); $("#button_edit_customer").click(function(evt){ alert($(this).attr("id")); alert($(this).data('id')); });});第一個問題:每個表第一行的按鈕顯示正確的值,但其他按鈕不顯示對話框。我想將每個表的 ID 傳遞給模態表單并執行查詢來填充表單。我想我可以設法將 ID 傳遞給模態,但是我應該如何以及何時執行查詢來填寫表單?這是我的表格:<div id="edit_customer" class="modal fade" role="dialog" data-backdrop="static"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header rtl"> <h4 class="modal-title">edit customer</h4> </div>
2 回答

慕沐林林
TA貢獻2016條經驗 獲得超9個贊
按照以下步驟
在按鈕單擊事件“edit_modal(id,name)”上調用函數
在你的頁面上寫一個函數
函數edit_modal(id,name){
// assign value in modal form
$('#form_edit_customer_id').val(id);
$('#form_edit_customer_name').val(name);
//Display modal
$('#edit_customer').modal('show');
}

忽然笑
TA貢獻1806條經驗 獲得超5個贊
我用了不同的方式來實現我的目標。這是我改編的鏈接: PHP Ajax Insert Data in MySQL By Using Bootstrap Modal
- 2 回答
- 0 關注
- 169 瀏覽
添加回答
舉報
0/150
提交
取消