且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用javascript showModalDialog将值从父窗体传递到子窗体

更新时间:2023-12-06 12:54:04

showModalDialog的中间参数可以是一个对象或一个数组或任何你喜欢的东西,这个传递的对象可以通过引用window.dialogArguments在子表单中(例如,在其OnLoad事件中)检索。

The middle parameter for showModalDialog can be an object or an array or anything you like, and this passed object can be retrieved in the child form (say, in its OnLoad event) by referencing window.dialogArguments.

暂停一下,我将包含一个代码示例(自我完成此操作以来已经过了大约10年)。

Hang on a second and I'll include a code sample (it's been about 10 years since I've done this).

更新:这是一个非常简单的代码示例,它显示了使用showModalDialog在父窗口和子窗口之间来回传递数据的基础知识。在同一文件夹中创建两个HTML文件,并将它们命名为Parent.htm和Child.htm。将此代码放入 Parent.htm

Update: here is a very simple code sample that shows the basics of passing data back and forth between the parent and child windows using showModalDialog. Create two HTML files in the same folder, and name them "Parent.htm" and "Child.htm". Put this code in Parent.htm:

<HTML>
<input type=button value="CustomConfirm" 
    onclick="ShowMyDialog()">
<script language="javascript">
function ShowMyDialog()
{
    var obj = new Object(); 
    obj.data1 = 'some data 1';
    obj.data2 = 'some data 2';
    showModalDialog('Child.htm', obj, '');
    alert(obj.returnvalue);
}
</script>
</HTML>

并将此代码放入 Child.htm

<HTML>
<body onload="ReadPassedData()" 
    onunload="DoUnload()">
<input type=text id="textbox1">
<br>
<input type=text id="textbox2">
<br>
<br>
Return value:<br>
<input type=text id="textbox3" 
    value="type something">
</body>
<script language="javascript">
function ReadPassedData()
{
    var obj = window.dialogArguments;
    var tb1 = document.getElementById('textbox1');
    tb1.value = obj.data1; 
    var tb2 = document.getElementById('textbox2');
    tb2.value = obj.data2; 
}
function DoUnload()
{
    var obj = window.dialogArguments;
    obj.returnvalue = textbox3.value;
}
</script>
</HTML>

然后在浏览器中打开Parent.htm并单击CustomConfirm按钮。子窗口将显示在父窗口中设置的值(某些数据1和某些数据2),当子窗口关闭时,您在第三个文本框中输入的任何内容都将显示在警报中从父母调用的框。这显示了将数据传递给子节点并从中获取数据的基本方法。

Then open Parent.htm in a browser and click the "CustomConfirm" button. The child window will display the values set in the parent window ("some data 1" and "some data 2"), and when the child window is closed, whatever you've entered in the third text box will be displayed in an alert box called from the parent. This shows the basic way in which you pass data to the child and get data back from it.

还有一种从子窗口返回对象的方法(后者变为从showModalDialog调用本身返回的值),但我不记得如何执行此操作。

There's also a way to return an object from the child window (which becomes the value returned from the showModalDialog call itself), but I don't recall how to do this.

更新2 :传递数组相反,你会做这样的事情:

Update 2: To pass an array instead, you would do something like this:

var myarray = new Array();
myarray[0] = "Bob Smith";
myarray[1] = "Doug Jones";
myarray[2] = "Englebert Humperdinck";
var ret = showModalDialog('Child.htm', myarray, '');
alert(ret); // this will display whatever the child set for its
    // window.returnValue

然后在子窗口中,您将像以前一样获得数组并使用它来构建您的详细信息显示:

And then in the child window, you would get the array like before and use it to build your details display:

var myarray = window.dialogArguments;
alert(myarray[0]); // or whatever

因为你现在传入一个数组而不是一个对象,所以你会需要返回true或false(而不是向传递的对象添加returnvalue属性)。您可以通过设置 window.returnValue 属性来设置子项中的返回值。由于你正在创建一个确认弹出窗口,你可能会有一个是和一个取消按钮,它将 window.returnValue 设置为 true false

Since you're now passing in an array instead of an object, you'll need to return true or false (instead of adding a returnvalue property to the passed object). You set the return value in the child by setting the window.returnValue property. Since you're creating a confirmation popup, you would presumably have a "Yes" and a "Cancel" button which would set window.returnValue to true or false, respectively.