简体   繁体   English

使用Twitter Bootstrap从JSON加载模式内容

[英]Load modal content from JSON with Twitter Bootstrap

Is there a way that I can load a Modal with data from a JSON response? 有没有一种方法可以用JSON响应中的数据加载Modal?

For example, 例如,

here's a link to sign out of my application: 这是退出我的应用程序的链接:

 <a tabindex="-1" href="/ajax/account/logout" data-toggle="modal" data-target="#ajax" class="modal-link">Sign Out</a>

In a normal circumstance, the response will be: 在正常情况下,响应将是:

 {"html":"htmlsource",
  "resultCode":1}

The html contains the HTML of the content to be put into the modal, so all I'd need to do is extract the html data and push it to the div container. html包含要放入模式中的内容的HTML,因此我需要做的就是提取html数据并将其推送到div容器。

Sure; 当然; for example, the html() jQuery function will shove its argument into whatever element you wish, including a Modal. 例如, html() jQuery函数会将其参数推入您想要的任何元素中,包括Modal。 In your case, assuming the "html" item has what you want to put in the Modal, you'd do something like $("#id-of-modal").html(response.html) . 在您的情况下,假设“ html”项具有要放入模态中的内容,您将执行类似$("#id-of-modal").html(response.html)

With this: 有了这个:

$.getJSON(url).success(function(data) {
   $('#ajax, div').replaceWith(data.html);
   $('#ajax').modal();
});

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

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