2017-05-05 100 views
0

我需要從子頁面調用主頁面中的函數。下面我有2頁。 1. main.aspx和子頁面是2.active.aspx。在active.aspx中,我有一個名爲「exit」的按鈕,當我點擊退出按鈕時,我需要關閉模式彈出窗口,然後我需要調用主要頁面中的JavaScript函數「main」。我不能夠從子頁面調用該函數。從子頁面的父頁面調用JavaScript函數

Main.aspx

<html> 
<head> 
    <script type="text/javascript"> 
     function main() { 
      //do main work 
     } 
    </script> 
</head> 
<body> 
    <div> 
     <input type="button" class="ic-grid-encode center-btn" title="Interactive" 
       data-toggle="modal" data-target="#divInterActiveContent" /> 
    </div> 
    <div class="modal fade" id="divInterActiveContent" tabindex="-1" role="dialog"> 
     <div class="modal-dialog" role="document" align-self="" center;"=""> 
      <iframe id="interId" src="active.aspx"></iframe> 
     </div> 
    </div> 
</body> 
</html> 

Active.aspx

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> 
    <link href="../Content/bootstrap.css" rel="stylesheet"> 
    <script src="../Scripts/jquery-3.1.1.js"></script> 
    <script src="../Scripts/bootstrap.js"></script> 
    <script src="../Scripts/knockout-3.4.2.js"></script> 
    <script src="../Scripts/jquery-ui-1.12.1.min.js"></script> 
    <script type="text/javascript"> 
     self.btnExit = function() { 
      parent.$('#divInterActiveContent').modal('hide'); 
      //after closing the popup i need to call the javascript function defined in the main page 
      //i.e i need call function main which is there in the main window. 
     } 
    </script> 
</head> 
<body> 
    <form id="frmInterActiveEncode"> 
     <div class="container"> 
      <div class="row" style="margin-top: 10px;"> 
      </div> 
      <hr> 
      <br> 
      <hr> 
      <div class="row"> 
       <input type="button" value="Submit" class="btn btn-primary" data-bind="event: { click: btnSubmit }"> 
       <input type="button" value="Exit" class="btn btn-primary" data-bind="event: { click: btnExit }"> 
      </div> 
     </div> 
    </form> 
</body> 
</html> 

回答

0

在您的場景中,您實際上首先關閉模式,即您也關閉iframe,因此在關閉模式後無法在子頁面中獲取父對象。

你應該做的像

active.aspx

self.btnExit = function() { 
    parent.main(); 
} 

main.aspx

function main(){ 
    $('#divInterActiveContent').modal('hide');   
    // Do your stuff 
} 

這樣你可以調用父功能,而不是藏在孩子隱藏它的模態在父母功能和你想要做的下一個東西。

+0

http://stackoverflow.com/questions/2379529/how-to-call-a-function-within-document-ready-from-outside-it –

相關問題