从外部源加载链接内部模态

load link inside modal from external source

本文关键字:内部 模态 链接 加载 从外部      更新时间:2023-09-26

我试图创建一个动态网站使用模态加载一些代码从其他来源,在这种情况下,我希望我的用户选择一个产品从模态视图使用链接,然后一个确认按钮像这样

主页->按钮->加载模态(模态源是外部页面("prods.php"))->使用按钮->模态内选择产品->模态现在显示("prod_detail.php") ->确认->数据返回到主页

这是我的模态加载外部源的实际代码,但我不能让它加载新的prod_detail,因为它在主窗口打开

//called when user clicks login
function login() {
    $("#main-username").val($("#modal-username").val());
    $("#main-result").val($("#modal-result").val());
    $("#myModal").modal("hide");
}
//called when the modal is closed, logs values grabbed from the modal in login()
$('#myModal').on('hidden', function() {
    console.log('username : '+$("#main-username").val());
    console.log('result : '+$("#main-result").val());
})
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<!-- button to trigger modal -->
<a href="prods.php" data-target="#myModal" data-toggle="modal">remote modal</a>
<!-- hidden fields to store modal result in -->
<input type="text" id="main-username">
<input type="text" id="main-result">
<!-- modal -->
<div id="myModal" 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">×</button>
    <h3 id="myModalLabel">Modal test</h3>
  </div>
  <div class="modal-body">
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button class="btn btn-primary" onclick="login();">Login</button>
  </div>
</div>​
</body>
</html>

谢谢

你的主窗口:

<!-- hidden fields to store modal result in -->
<input type="text" id="main-username">
<input type="text" id="main-result">
<script>
window.setData = function(data) {
  console.log('data from iframe', data);
} 
</script>
<!-- modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <iframe src="/prods.php"></iframe>
</div>​

…prods.php

  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal test</h3>
  </div>
  <div class="modal-body">
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button class="btn btn-primary" onclick="window.parent.setData("HI from iframe")">Say Hi to Main Window</button>
  </div>