简体   繁体   English

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

[英]Twitter Bootstrap Modal change Content via Ajax

I know, here are many question with the same topic. 我知道,这里有很多关于同一主题的问题。 But I cannot find a solution for my prpblem. 但是我找不到我的问题的解决方案。 So I have a layout and before the body tag closes there is the modal window: 所以我有一个布局,并且在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>

I have a link in my site that toggles the modal window: 我的网站上有一个链接可以切换模式窗口:

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

My remote php only contains " <php echo $_GET['id'];?> " for testing. 我的远程php仅包含“ <php echo $_GET['id'];?> ”进行测试。 In the future there is content from a database. 将来会有来自数据库的内容。

So everytime I click on the link to the modal the content will change. 因此,每次我单击模态链接时,内容都会更改。 this is not the problem, I know how to do this in php etc. 这不是问题,我知道如何在php等中执行此操作。

And I write JS that the modal content should change: 我写了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"));
        });
    })

the problem: 问题:

when I click the link the modal window opens. 当我单击链接时,将打开模式窗口。 perfect. 完善。 But than the modal window dissappear and in the top position of my site there is the "1". 但是,除了模式窗口消失之外,在我网站的顶部位置还有“ 1”。 But this is not so nice, because I only want that the content "CONTENT" in the modal-body should change to "1." 但这不是很好,因为我只希望模态主体中的内容“ CONTENT”更改为“ 1”。 What I am doing wrong here? 我在这里做错了什么?

And - another question. 还有-另一个问题。 If I do this with changing content every time with the $_GET['id'] the 1 never dissappear, but I want changing the content dynamic here, so in this example there should be a "2" in the modal-body class of the modal window if I click on a link like this: 如果我每次使用$ _GET ['id']更改内容时都不会消失1,但是我想在此处更改内容动态,因此在此示例中,模态主体类中应有一个“ 2”模式窗口,如果我单击这样的链接:

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

Can somebody help me please? 有人可以帮我吗?

Make link like below: 制作如下链接:

<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>

On Js: 在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 );

        });
    })

Can you try it ? 你可以试试看吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM