$(document).ready(function () { // 編輯的 Modal 事件 $('#editModal').on('show.bs.modal', function (event) { var btn = $(event.relatedTarget) var title = btn.data('title') var modal = $(this) modal.find('.modal-title').text(title)
// get hidden input var tr = btn.parent().parent().parent() var hiddenInput = tr.find('td input')[0] console.log('hiddenInput', hiddenInput)
// get id from hidden_input & set id to modal span var id = hiddenInput.value console.log('id', id) modal.find('.modal-id').text(id) modal.find('.payment-btn')[0].setAttribute('data-id', id)
console.log($('table')) })
// 主要看這裡 $('.payment-btn').on('click', function (event) { console.log('this', $(this))
// fail, data is old // var btn = $(this); // var id = btn.data('id');
console.log('way 1', $(this).data('id')) // old value console.log('way 2', event.target.dataset.id) // new console.log('way 3', this.dataset.id) // new console.log('way 4', $(this).attr('data-id')) // new }) })