9

Bootstrap のモーダル プラグインを介して、1 つのモーダル div を再利用するページ上の多数の異なるリンクを使用できるようにしたいと考えています。

<h3>This button shows a modal (<code>#utility</code>) with text "Phasellus <em>tincidunt gravida</em>..."</h3>
<br />
<a id="file_attach" data-toggle="modal" href="http://fiddle.jshell.net/jhfrench/6K8rD/show/" data-target="#utility" class="btn">Modal 1</a><br />
<h3>This button should invoke the same modal (<code>#utility</code>), but fill it with text "Phasellus <em>egestas est eu</em>..."</h3>
<br />
<a id="file_attach" data-toggle="modal" href="http://fiddle.jshell.net/jhfrench/AWSnt/show/" data-target="#utility" class="btn">Modal 2</a><br />

<!-- Modal -->
<div id="utility" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3 id="myModalLabel">Click outside modal to close it</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…this is getting replaced with content that comes from passed-in href</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>

hrefいずれかのリンク (ボタンとしてスタイル設定されている) をクリックすると、モーダルが呼び出され、クリックされたボタンに対応するページ (の値) がモーダルに読み込まれると思います。代わりに、モーダルは、最初にクリックしたリンクのコンテンツを常にロードします。hrefモーダルは、「その他」のリンクによって指示されるべきコンテンツで「更新」されません。

これは Bootstrap のバグだと思いますが、間違った使い方をしている場合に備えて、ここで質問しています。

デモンストレーションについては、http://jsfiddle.net/jhfrench/qv5u5/を参照してください。

4

2 に答える 2

15

さらに調査した結果、この動作に関するいくつかの Bootstrap の問題が見つかりましたここここ. 問題 5514の再開を依頼しました。

その間、この jQuery は問題を修正します*:

$('a[data-toggle="modal"]').on('click', function(){
    // update modal header with contents of button that invoked the modal
    $('#myModalLabel').html( $(this).html() );
    //fixes a bootstrap bug that prevents a modal from being reused
    $('#utility_body').load(
        $(this).attr('href'),
        function(response, status, xhr) {
            if (status === 'error') {
                //console.log('got here');
                $('#utility_body').html('<h2>Oh boy</h2><p>Sorry, but there was an error:' + xhr.status + ' ' + xhr.statusText+ '</p>');
            }
            return this;
        }
    );
});​

実際の例については、http://jsfiddle.net/jhfrench/qv5u5/51/を参照してください。

*-何らかの理由で、フィドルのボタンをクリックすると、モーダルが空白で表示されることがあります。これは私が使用しているアプリケーションの問題ではないため、この質問/回答とは無関係の問題であると思われます。

于 2012-12-27T18:11:50.823 に答える
5

モーダルの HTML マークアップを記述して JavaScript で操作する代わりに、Bootstrap-Dialog を使用してすべてを簡単にします。

$('.modal-btn').click(function(event){
    var $link = $(this);
    new BootstrapDialog({
        title   :   'Load content of : ' + $link.attr('href'),
        content :   $('<div>Loading...</div>').load($link.attr('href')),
        buttons :   [{
            label   :   'Close',
            onclick :   function(dialog){
                dialog.close();
            }
        }, {
            label   :   'Save changes',
            cssClass:   'btn-primary',
            onclick :   function(dialog){
                alert('The content of the dialog is: ' + dialog.getBody().html());
                dialog.close();
            }
        }]
    }).open();

    event.preventDefault();
});

ライブデモについては、こちらを参照してください。例で提供した2つのURLをロードします: http://jsfiddle.net/txrM8/

Bootstrap-Dialog の詳細については、http: //nakupanda.github.io/bootstrap-dialog/を参照してください。

于 2013-06-07T09:30:49.880 に答える