JSONフィードからエントリのリストを生成しています。 divをクリックして編集モーダルを表示したい私はclick
イベントからrelatedTargetを取得する際に問題が発生しています。これは未定義として返されます。divをクリックしたときにフォームを表示
データをモーダルに渡す別の方法はありますか?あなたの動的に作成.panel
要素のための
$.getJSON('api/v1/module', function(data){
$.each(data, function(i, learning) {
var $div = $('<div>')
.append(
$('<p>').text(learning.title),
$('<p>').text(learning.lastupdated)
)
.addClass('panel panel-default')
.attr('data-title', learning.title)
.appendTo('.module-list')
.on('click', function(){
$('#edit-module').modal({
show: true
})
})
});
})
$('#edit-module').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('title') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-body input').val(recipient)
console.log(event.relatedTarget)
console.log(event)
console.log(button)
console.log(JSON.stringify(button))
})
これをモーダルのショーイベントにフックすると、それをソートしました。ありがとう。私はあなたを得ている間。 divなどにJSONデータ/ IDを格納するためのより良い方法はありますか?私はそれ以降の使用のために関係データを保存するのが難しいと思う。 –
喜んで助けました。データ属性にデータを格納したり、作成中の実際のコンテンツからデータを読み取ったりすることができます。例:var id = button.attr( 'id')、title = $( 'p.title'、ボタン).text(); '。 JSONとして別に書くこともできます。 –