JavaScript example: 弹出窗口,并且相互传值

窗口之间的传值当然要用到dialogArguments,看api英文解释
dialogArguments Property:
Retrieves the variable or array of variables passed into the modal dialog window.
Remarks(备注)
The [b]dialogArguments [/b]property applies [b]only [/b]to windows created using the showModalDialog and showModelessDialog methods.


主要步骤或者思路就是:
[b]1 父窗口parent.htm调用[/b] window.showModalDialog("children.htm", myObject, "dialogHeight:300px; dialogLeft:200px;");
或者window.showModalDialog("children.htm", window, "dialogHeight:300px; dialogLeft:200px;");真正实际当中第二种用得比较的多,除了局部变量其它所有的值都可以得到.


[b]2 子窗口children.htm [/b]
[b]2.1 子窗口显示[/b]: 调用window.dialogArguments可以得到传过来的值,如果传过来的是window,那么父页面的所有元素,子窗口都可以得到(除了局部变量),得到之后就可以进行显示了,
[b]2.2 子窗口修改值并返回:[/b]
window.dialogArguments.firstName = document.getElementById('a').value;
window.dialogArguments.lastName = document.getElementById('b').value;
window.close();
父窗口就可以得到子窗口修改过的值
如果传过来的是window,就可以这样用:
window.dialogArguments.document.getElementById("searchbutton").click();


先上效果图,
[img]http://dl.iteye.com/upload/picture/pic/56112/b22f0775-b27e-3a5a-9ed8-8cc8d358e295.bmp[/img]

以下是例子代码注释很详细:
parent.htm
<HTML>
<HEAD>
<SCRIPT>
// var bb = 'oyp';
//in the new modal window Like this 'window.dialogArguments.bb' can get this value

function fnLaunch() {
var aForm;
aForm = oForm.elements;
var myObject = new Object();
myObject.firstName = aForm.oFirstName.value;
myObject.lastName = aForm.oLastName.value;


//The object "myObject" is sent to the modal window.
//It can also be written like this:
//window.showModalDialog("x.htm",window, "dialogHeight:300px; dialogLeft:200px;");
//The window parameter is passed in the method
//in the new modal window. alert(window.dialogArguments.oForm.elements.oFirstName.value);
//alert(window.dialogArguments.bb); 当然这个var bb 必须是全局变量才能这样得到,局部变量不行
// if like this
//var bb = '1111111111';
//window.showModalDialog("b.htm", bb, "dialogHeight:300px; dialogLeft:200px;");
//alert(window.dialogArguments); 可以得到这个局部变量
// The object "myObject" is sent to the modal window.
// Here, I just pass this object in the method only!!
//在children.htm可以对myObject进行处理,然后返回

window.showModalDialog("children.htm", myObject, "dialogHeight:300px; dialogLeft:200px;");
if (myObject.firstName != '') {
document.getElementById('oFirstName').value = myObject.firstName;
} if (myObject.lastName != '') {
document.getElementById('oLastName').value = myObject.lastName;
}
}
</SCRIPT>
</HEAD>
<BODY>
<BUTTON onclick="fnLaunch();" >Launch The Window</BUTTON>
<FORM ID= "oForm">
First Name:
<INPUT TYPE="text" NAME="oFirstName" VALUE="Jane">
<BR>
Last Name:
<INPUT TYPE="text" NAME="oLastName" VALUE="Smith">
</FORM>
</BODY>
</HTML>



children.htm
<HTML>
<HEAD>
<SCRIPT>
var oMyObject = window.dialogArguments; //得到父窗口传递过来的对象
var sFirstName = oMyObject.firstName;
var sLastName = oMyObject.lastName;
</SCRIPT>
<title>Untitled</title>
</head>
<BODY STYLE="font-family: arial; font-size: 14pt; color: Snow;background-color: RosyBrown;">
<!--显示父窗口传递过来的数据-->
First Name:
<SPAN STYLE="color:00ff7f">
<SCRIPT>
document.write(sFirstName);
</SCRIPT>
</SPAN>
<BR>
Last Name:
<SPAN STYLE="color:00ff7f">
<SCRIPT>
document.write(sLastName);
</SCRIPT>
</SPAN>
<!-- end -->

<!--子窗口设置父窗口传递过来的值并进行返回-->
<script>
function setValue() {
window.dialogArguments.firstName = document.getElementById('a').value;
window.dialogArguments.lastName = document.getElementById('b').value;
window.close();
}
</script>
<br>
First Name:<input type ='text' id = 'a' ><br>
Last Name: <input type ='text' id = 'b' ><br>
<input type = 'button' value = '提交' onclick = 'setValue()' >
<input type = 'button' value = '返回' onclick = 'JavaScript:window.close()' >
<!--end-->
</BODY>
</HTML>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值