Twitter引导模式通过Ajax更改内容

Twitter Bootstrap Modal change Content via Ajax

本文关键字:Ajax 模式 Twitter      更新时间:2023-09-26

我知道,这里有很多相同主题的问题。但我找不到解决问题的办法。所以我有一个布局,在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 );
        });
    })

你能试试吗?