我在 td 中有一個編輯按鈕,單擊該按鈕會打開一個模式。模態主體中的表單從外部文件加載。我想將行 id 值傳遞給表單。在我的嘗試中,id 不會在第一次點擊時傳遞(它返回未定義),但在后續點擊中它工作得很好。這是 HTML 代碼: <button type="button" class="btn" id="edit" data-id="<?php echo $row['id'] ;?>"></button>腳本: $(document).ready(function() { $("#tableCountries tbody tr td button.btn").on("click", function() { if ($(this).attr("id") == "edit") { //fetch row id from button var rowid = $(this).data('id'); //open modal var modal = $("#modalCountries").modal(); //load external form into modal body modal.find('.modal-body').load("modal.html #countries_form"); //add row id value to countries form $("#countries_form").attr('data-value', rowid); //check value of id var newid = $("#countries_form").data('value'); alert(newid); } }); });我嘗試過使用bind()或live()而不是on(),我嘗試過使用data()而不是attr()來分配id,我也嘗試過將load()放在if語句之外。這些都不起作用。任何幫助,將不勝感激。
第一次嘗試向元素添加數據屬性不起作用
浮云間
2023-07-29 13:50:48