亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

第一次嘗試向元素添加數據屬性不起作用

第一次嘗試向元素添加數據屬性不起作用

浮云間 2023-07-29 13:50:48
我在 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語句之外。這些都不起作用。任何幫助,將不勝感激。
查看完整描述

1 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

.load()是異步的,因此您必須在完成后使用其回調來運行代碼。

modal.find('.modal-body').load("modal.html #countries_form", function(){
    $("#countries_form").attr('data-value', rowid);
});


查看完整回答
反對 回復 2023-07-29
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號