在Web开发中,常常要用到两个窗口之间互相传值。下面谈谈父子窗口之间的传值:
一:使用Open开启子窗口
1:单值传递
通过open开启的子窗口比较好处理。
页面窗口1.html代码:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<body>
<form name=”dForm” id=”dForm” method=”post” οnsubmit=”return dFormCK();” action=”abc.php”>
<input type=”text” size=”30″ name=”p” id=”p” value=””/>
</form>
<a href=”javascript:void(0)” οnclick=”window.open(‘2.html’,’child’,’width=400,height=300,left=200,top=200′);”>打开子窗口</a>
</body>
</html>
Open后弹出的子窗口2.html:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<base target=”_self”>
<body>
<input type=”button” οnclick=”JavaScript:window.opener.document.getElementById(‘p’).value=’ok’;window.close();” value=”确定”>
</body>
2:多值传递
多值的值的传递与单值传递是一模一样的。
页面窗口1.html代码:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<body>
<form name=”dForm” id=”dForm” method=”post” οnsubmit=”return dFormCK();” action=”abc.php”>
<input type=”text” size=”30″ name=”p0″ id=”p0″ value=””/><br />
<input type=”text” size=”30″ name=”p1″ id=”p1″ value=””/>
</form>
<a href=”javascript:void(0)” οnclick=”window.open(‘2.html’,’child’,’width=400,height=300,left=200,top=200′);”>打开子窗口</a>
</body>
</html>
Open后弹出的子窗口2.html:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<base target=”_self”>
<body>
<input type=”button” οnclick=”JavaScript:window.opener.document.getElementById(‘p0′).value=’值一’;window.opener.document.getElementById(‘p1′).value=’值二’;window.close();” value=”确定”>
</body>
二:使用showModalDialog开启子窗口
1:单值传递
由于window.showModalDialog 打开的子窗口,不支持 window.opener属性,因此,我们使用一参数方式来传值。具体操作如下:
页面文件1.html
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<script language=”JavaScript”>
<!–
function show()
{
var a=window.showModalDialog(‘2.html’,”pwin”,’dialogWidth:480px;dialogHeight:460px;center:yes;resizable:no;scroll:no’);
document.dForm.p.value=a;
}
//–>
</script>
<body>
<form name=”dForm” id=”dForm” method=”post” οnsubmit=”return dFormCK();” action=”abc.php”>
<input type=”text” size=”30″ name=”p” id=”p” value=””/>
</form>
<a href=”javascript:void(0);” οnclick=”show();”>ShowModelDialog</a>
</body>
</html>
弹出的子窗口2.html:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<base target=”_self”>
<body>
<input type=”button” οnclick=”JavaScript:window.returnValue=’这是返回的值’;window.close();” value=”确定”>
<input type=”button” οnclick=”JavaScript:window.returnValue=”;window.close();” value=”取消”>
</body>
2:多值传递
多值的传递方法同第一点,也是通过一个参数来传递,由于是多个值,所以我们自然而然地想到要用数组来传递。
页面文件11.html
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<script language=”JavaScript”>
<!–
function show()
{
var a=new Array(3);
var a=window.showModalDialog(’22.html’,”pwin”,’dialogWidth:480px;dialogHeight:460px;help:no;center:yes;resizable:no;scroll:no’);
document.dForm.p0.value=a[0];
document.dForm.p1.value=a[1];
document.dForm.p2.value=a[2];
}
//–>
</script>
<body>
<form name=”dForm” id=”dForm” method=”post” οnsubmit=”return dFormCK();” action=”abc.php”>
<input type=”text” size=”30″ name=”p0″ id=”p0″ value=””/><br />
<input type=”text” size=”30″ name=”p1″ id=”p1″ value=””/><br />
<input type=”text” size=”30″ name=”p2″ id=”p2″ value=””/>
</form>
<a href=”javascript:void(0);” οnclick=”show();”>ShowModelDialog</a>
</body>
</html>
弹出子窗窗口22.html
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<base target=”_self”>
<body>
<input type=”button” οnclick=”JavaScript:var s=Array(3);s[0]=’abc’;s[1]=’bcd’;s[2]=’cde’; window.returnValue=s;window.close();” value=”确定”>
</body>