簡體   English   中英

Twitter引導模式通過Ajax更改內容

[英]Twitter Bootstrap Modal change Content via Ajax

我知道,這里有很多關於同一主題的問題。 但是我找不到我的問題的解決方案。 所以我有一個布局,並且在body標簽關閉之前,有一個模態窗口:

<!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <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">Modal title</h4>
              </div>
              <div class="modal-body">
                CONTENT
              </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>
            </div>
          </div>
        </div>
    </body>

我的網站上有一個鏈接可以切換模式窗口:

<a data-toggle="modal" class="btn btn-info" href="myRemote.php?id=1" data-target="#myModal">Click</a>

我的遠程php僅包含“ <php echo $_GET['id'];?> ”進行測試。 將來會有來自數據庫的內容。

因此,每次我單擊模態鏈接時,內容都會更改。 這不是問題,我知道如何在php等中執行此操作。

我寫了JS,應該更改模式內容:

$(document).ready(function() {
        // Fill modal with content from link href
        $("#myModal").on("show.bs.modal", function(e) {
            var link = $(e.relatedTarget);
            $(this).find(".modal-body").load(link.attr("href"));
        });
    })

問題:

當我單擊鏈接時,將打開模式窗口。 完善。 但是,除了模式窗口消失之外,在我網站的頂部位置還有“ 1”。 但這不是很好,因為我只希望模態主體中的內容“ CONTENT”更改為“ 1”。 我在這里做錯了什么?

還有-另一個問題。 如果我每次使用$ _GET ['id']更改內容時都不會消失1,但是我想在此處更改內容動態,因此在此示例中,模態主體類中應有一個“ 2”模式窗口,如果我單擊這樣的鏈接:

<a data-toggle="modal" class="btn btn-info" href="myRemote.php?id=2" data-target="#myModal">Click</a>

有人可以幫我嗎?

制作如下鏈接:

<a class="btn btn-info openModal" data-href="myRemote.php?id=1">Click</a>
<a class="btn btn-info openModal" data-href="myRemote.php?id=1">Click 2</a>

在Js上:

$(document).ready(function() {
        // Fill modal with content from link href
        $(".openModal").on("click", function(e) {
            var link = $(this).data("href");
        $('#myModal').modal("show");
        $('#myModal .modal-body').load(link );

        });
    })

你可以試試看嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM