归纳-子窗体传值到父窗体
第一种:Open
![]()
父窗体
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4
<title>父窗体</title>
5
</head>
6
<body>
7
<form name="form1" method='get'>
8
<input type=text name='txtvalue' value=''>
9
<input type='button' onclick='openWindow()' value="弹出">
10
</form>
11![]()
<script type="text/javascript">
12![]()
function openWindow() {
13
window.open(子窗体.html','newWindow','height=300,width=300,scrollbars=auto');
14
}
15
function setValue(returnValue) {
16
document.getElementById('txtvalue').value= returnValue;
17
}
18
</script>
19![]()
20
</body>
21
</html>
![]()
子窗体
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4
<title>子窗口</title>
5![]()
<script type="text/javascript">
6![]()
7![]()
function valueExit() {
8
var returnValue = docment.getElementById('txtValue').value;
9
opener.setValue(returnValue);
10
window.close();
11
}
12
</script>
13
</head>
14
<body>
15
<h1 align='center'>子窗口!</h1><hr>
16
<div align='center'>
17
<form name="fz" action='#' method='get'>
18
<input type=text name='txtValue' id='txtValue'>
19
<input type='button' value='关闭' onclick='valueExit()'/>
20
</form>
21
</div>
22
</body>
23
</html> 2.在子窗体里面用window.opener.document.from方法传值
![]()
父窗体
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4
<title>父窗体</title>
5
</head>
6
<body>
7
<form name="f1" action='#' method='get'>
8
<input type=text name='ft' value=''>
9
<input type='button' onclick='openWindow()' value="弹出"/>
10
</form>
11![]()
<script type="text/javascript">
12
<!--
13![]()
function openWindow() {
14
newWindow = window.open('z1.html','newWindow','height=300,width=300,scrollbars=auto');
15![]()
if (newWindow != null) {
16
newWindow.focus();
17
}
18
}
19
//-->
20
</script>
21
</select>
22
</body>
23
</html>
![]()
Code
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4
<title>子窗口</title>
5
</head>
6
<body>
7
<h1 align='center'> 子窗口!</h1><hr>
8
<div align='center'>
9
<form name="fz" action='#' method='get'>
10
<input type=text name='tt'>
11
<input type='button' value='关闭' onclick='window.opener.document.f1.ft.value=document.fz.tt.value;self.close();' />
12
</form>
13
</div>
14
</body>
15
</html>
扩展:子窗体关闭要刷新父窗体,在子窗体赋值的第一句加上opener.location= opener.location.href;
![]()
父窗体
1
<html>
2
<head>
3![]()
<script type="text/javascript">
function fnOpen() {
4
var returnTarget = window.showModalDialog("zichuangti.aspx", 'newWindow', 'dialogHeight=500px;dialogWidth=700px;center=yes;resizable=yes;help=no;');
5![]()
if (returnTarget != undefined && returnTarget.length > 0) {
6
document.getElementById("oArgs").value=returnTarget;
7
}
}
8
</script>
9
</head>
10
<body>
11
<form id="form1" runat="server">
12
<div>
13
传过来的值: <input type="text" id="oArgs" runat ="server"/>
14
<input id="button" name="button" type="button" value="打开窗口" runat ="server" onclick="fnOpen()"/>
15
</div>
16
</form>
17![]()
18
</body>
19
</html>
20![]()
21![]()
22![]()
![]()
Code
<html>
<head>
<script type="text/javascript">
function exit()
{
window.returnValue = form1.txtreturn.value; //返回值
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
传递过来的值: <input type="text" id="txtreturn" runat ="server"/>
<input id="button" name="button" type="button" value="关闭窗口" runat ="server" onclick="exit()"/>
</div>
</form>
</body>
</html>
扩展:子窗体关闭要刷新父窗体,在打开窗体的URL后面加上 window.location.reload();
1. 通过Opener方法调用父窗体方法传值
1
<html>2
<head>3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">4
<title>父窗体</title>5
</head>6
<body>7
<form name="form1" method='get'>8
<input type=text name='txtvalue' value=''>9
<input type='button' onclick='openWindow()' value="弹出">10
</form>11

<script type="text/javascript"> 12

function openWindow() {13
window.open(子窗体.html','newWindow','height=300,width=300,scrollbars=auto'); 14
} 15
function setValue(returnValue) {16
document.getElementById('txtvalue').value= returnValue;17
}18
</script> 19

20
</body>21
</html> 1
<html>2
<head>3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">4
<title>子窗口</title>5

<script type="text/javascript">6

7

function valueExit() {8
var returnValue = docment.getElementById('txtValue').value;9
opener.setValue(returnValue); 10
window.close();11
}12
</script>13
</head>14
<body>15
<h1 align='center'>子窗口!</h1><hr>16
<div align='center'>17
<form name="fz" action='#' method='get'>18
<input type=text name='txtValue' id='txtValue'>19
<input type='button' value='关闭' onclick='valueExit()'/> 20
</form>21
</div>22
</body>23
</html>1
<html>2
<head>3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">4
<title>父窗体</title>5
</head>6
<body>7
<form name="f1" action='#' method='get'>8
<input type=text name='ft' value=''>9
<input type='button' onclick='openWindow()' value="弹出"/>10
</form>11

<script type="text/javascript"> 12
<!-- 13

function openWindow() {14
newWindow = window.open('z1.html','newWindow','height=300,width=300,scrollbars=auto'); 15

if (newWindow != null) {16
newWindow.focus(); 17
} 18
}19
//--> 20
</script> 21
</select>22
</body>23
</html> 1
<html>2
<head>3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">4
<title>子窗口</title>5
</head>6
<body>7
<h1 align='center'> 子窗口!</h1><hr>8
<div align='center'>9
<form name="fz" action='#' method='get'>10
<input type=text name='tt'>11
<input type='button' value='关闭' onclick='window.opener.document.f1.ft.value=document.fz.tt.value;self.close();' />12
</form>13
</div>14
</body>15
</html>扩展:子窗体关闭要刷新父窗体,在子窗体赋值的第一句加上opener.location= opener.location.href;
第二种:ShowModalDialog
1.window.returnValue方法传值
1
<html>2
<head>3

<script type="text/javascript"> function fnOpen() {
4
var returnTarget = window.showModalDialog("zichuangti.aspx", 'newWindow', 'dialogHeight=500px;dialogWidth=700px;center=yes;resizable=yes;help=no;');5

if (returnTarget != undefined && returnTarget.length > 0) {6
document.getElementById("oArgs").value=returnTarget;7
}}
8
</script>9
</head>10
<body>11
<form id="form1" runat="server">12
<div>13
传过来的值: <input type="text" id="oArgs" runat ="server"/>14
<input id="button" name="button" type="button" value="打开窗口" runat ="server" onclick="fnOpen()"/>15
</div>16
</form>17

18
</body>19
</html>20

21

22

<html>
<head>
<script type="text/javascript">
function exit()
{
window.returnValue = form1.txtreturn.value; //返回值
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
传递过来的值: <input type="text" id="txtreturn" runat ="server"/>
<input id="button" name="button" type="button" value="关闭窗口" runat ="server" onclick="exit()"/>
</div>
</form>
</body>
</html>


浙公网安备 33010602011771号