javascript实现打开窗体以及窗体间传值

父窗体js代码(打开子窗体):

js_open
 1 <script type="text/javascript">
 2         //打开模式对话框 取得返回值
 3         function OpenModalDialog() {
 4             //用一个value来接受返回值
 5             var value = window.showModalDialog("ModalDialogChild.aspx""ModalDialog""dialogWidth=800px; dialogHeight=1000px; center:Yes; Help:No; Resizable:No; Status:no; edge:sunken");
 6             $("#txtMsg").val(value);
 7         }
 8 
 9         //通过window.open打开窗体 取得返回值
10         function OpenWin() {
11             window.open("winChild.aspx""window""height=800,width=1000,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=no,status=no");
12         }
13     </script>

父窗体页面html:

html
 1 <body>
 2     <form id="form1" runat="server">
 3     <div>
 4         <input type="button" id="btn" value="OpenModalDialog" onclick="OpenModalDialog();" />
 5         <br />
 6         <br />
 7         ModalDialog:<asp:TextBox ID="txtMsg" runat="server"></asp:TextBox>
 8         <br />
 9         <br />
10         <input type="button" id="Button1" value="OpenWin" onclick="OpenWin();" />
11         <br />
12         <br />
13         OpenWin:<asp:TextBox ID="txtWinMsg" runat="server"></asp:TextBox>
14     </div>
15     </form>
16 </body>
子窗体_模式对话框:
ModalDialog
 1 <head runat="server">
 2     <title></title>
 3     <script type="text/javascript">
 4         function GetValue() {
 5             //通过window.returnValue来返回值到父窗体 由父窗体中定义的value接收
 6             window.returnValue = document.getElementById("txtMsg").value;
 7             window.close();
 8         }
 9     </script>
10 </head>
11 <body>
12     <form id="form1" runat="server">
13     <div>
14         <asp:TextBox ID="txtMsg" runat="server" onblur="GetValue();"></asp:TextBox>
15     </div>
16     </form>
17 </body>
子窗体_Window.Open页面:
window.open
 1 <head runat="server">
 2     <title></title>
 3     <script type="text/javascript">
 4         function SetWinValue() {
 5             //window.opener为打开该页面的父页面引用 可以用过引用来获取父窗体的控件
 6             window.opener.document.getElementById("txtWinMsg").value = document.getElementById("txtMsg").value;
 7             //下面语句为了防止关闭页面时 出现关闭提示
 8             window.opener = null;
 9             window.open('''_self''');
10             //关闭
11             window.close(); 
12         }
13     </script> 
14 </head>
15 <body>
16     <form id="form1" runat="server">
17     <div>
18         <input type="text" id="txtMsg" />
19         <input type="button" id="btn" value="给父窗体赋值" onclick="SetWinValue();" />
20     </div>
21     </form>
22 </body>

 

posted @ 2012-10-30 15:30  HolyKnight  阅读(550)  评论(0编辑  收藏  举报