Help us understand the problem. What is going on with this article?

bootstrap3で任意のURLをモーダル内に読み込む

More than 1 year has passed since last update.

動的にモーダルの中身を変更したい場合があります。
そんな時の方法についてご紹介。

まずはモーダルを表示したい方のHTML。
モーダルが開くタイミングで、show.bs.modalでイベントをひろって
data-hrefで設定したURLを読み込ませています。

sample.html
<a href="#" data-href="load.html" data-toggle="modal" data-target="#modalDetails">リンク</a>

<!-- Modal -->
<div class="modal fade" id="modalDetails" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
    </div>
  </div>
</div>

<script>
$(function () {
    //任意のリンクをモーダル内に読み込む
    $("#modalDetails").on("show.bs.modal", function(e) {
        var link = $(e.relatedTarget); //クリックしたセルのオブジェクトデータ
        $(this).find(".modal-content").load(link.attr("data-href"));
    });
});
</script>

動的に表示するためのHTMLです。

load.html
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">タイトル</h4>
</div>
<div class="modal-body">
動的にいれたい内容を挿入
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
plus_i
長崎県五島出身。福岡にてWebサイト制作・iPhoneアプリ開発をしているフリーランスです。
http://plus.vc/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした