javascript - 从外部源加载modal内部的链接

时间:2016-08-11 20:52:53

标签: javascript jquery html modal-dialog bootstrap-modal

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

主页 - >按钮 - >加载模态(模态源是外部页面(“prods.php”)) - >使用按钮选择模态内的产品 - > modal现在显示(“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>

由于

1 个答案:

答案 0 :(得分:0)

您的主窗口:

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