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

本文介绍了一个使用JavaScript实现父窗体与子窗体间数据传递的例子。通过`window.showModalDialog`和`window.open`方法打开子窗体,并利用`window.returnValue`及`window.opener`属性将数据从子窗体返回给父窗体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif html
 1 <body>
 2     <form id= " form1 " runat= " server ">
 3     <div>
 4         <input type= " button " id= " btn " value= " OpenModalDialog " οnclick= " 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 " οnclick= " OpenWin(); " />
11         <br />
12         <br />
13         OpenWin:<asp:TextBox ID= " txtWinMsg " runat= " server "></asp:TextBox>
14     </div>
15     </form>
16 </body>
子窗体_ 模式对话框:
ExpandedBlockStart.gif 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 " οnblur= " GetValue(); "></asp:TextBox>
15     </div>
16     </form>
17 </body>
子窗体 _Window.Open页面:
ExpandedBlockStart.gif 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= " 给父窗体赋值 " οnclick= " SetWinValue(); " />
20     </div>
21     </form>
22 </body>

 

转载于:https://www.cnblogs.com/holyknight-zld/archive/2012/10/30/openwin.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值