Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - 将数据传递给bootstrap模式(Passing data to a bootstrap modal)

I've got a couple of hyperlinks that each have an ID attached.

(我有几个超链接,每个超链接都附有一个ID。)

When I click on this link, I want to open a modal ( http://twitter.github.com/bootstrap/javascript.html#modals ), and pass this ID to the modal.

(当我点击此链接时,我想打开一个模态( http://twitter.github.com/bootstrap/javascript.html#modals ),并将此ID传递给模态。)

I searched on google, but I couldn't find anything that could help me.

(我搜索谷歌,但我找不到任何可以帮助我的东西。)

This is the code:

(这是代码:)

<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>

Which should open:

(哪个应该打开:)

<div class="modal hide" id="addBookDialog">
    <div class="modal-body">
        <input type="hidden" name="bookId" id="bookId" value=""/>
    </div>
</div>

With this piece of code:

(有了这段代码:)

$(document).ready(function () {
    $(".open-AddBookDialog").click(function () {
        $('#bookId').val($(this).data('id'));
        $('#addBookDialog').modal('show');
    });
});

However, when I click the hyperlink, nothing happens.

(但是,当我单击超链接时,没有任何反应。)

When I give the hyperlink <a href="#addBookDialog" ...> , the modal opens just fine, but it does't contain any data.

(当我给超链接<a href="#addBookDialog" ...> ,模态打开就好了,但它不包含任何数据。)

I followed this example: How to pass values arguments to modal.show() function in Bootstrap

(我按照这个例子: 如何将值参数传递给Bootstrap中的modal.show()函数)

(and I also tried this: How to set the input value in a modal dialogue? )

((我也试过这个: 如何在模态对话框中设置输入值? ))

  ask by Leon Cullens translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>&nbsp;</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');
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...