I think you can make this work using jQuery's .on event handler.
(我认为你可以使用jQuery的.on事件处理程序来完成这项工作。)
Here's a fiddle you can test;
(这是你可以测试的小提琴;)
just make sure to expand the HTML frame in the fiddle as much as possible so you can view the modal. (只需确保尽可能扩展小提琴中的HTML框架,以便查看模态。)
http://jsfiddle.net/Au9tc/605/
(http://jsfiddle.net/Au9tc/605/)
HTML
(HTML)
<p>Link 1</p>
<a data-toggle="modal" data-id="ISBN564541" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<p> </p>
<p>Link 2</p>
<a data-toggle="modal" data-id="ISBN-001122" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value=""/>
</div>
</div>
JAVASCRIPT
(JAVASCRIPT)
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val( myBookId );
// As pointed out in comments,
// it is unnecessary to have to manually call the modal.
// $('#addBookDialog').modal('show');
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…