使用javascript showModalDialog将值从父窗体传递到子窗体 [英] Pass Value From Parent Form to Child Form using javascript showModalDialog

查看:92
本文介绍了使用javascript showModalDialog将值从父窗体传递到子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想传递我的价值确认框!!!所以我想说我要删除1号项目,所以当我按下删除按钮时,我选中了复选框。弹出框中出现了详细项目1。

if i want to pass in my value for confirmation box!!! so lets say i want to delete item no 1 so i checked the check box then when i pressed the delete button. the popup box come out with detail item no 1 in it.

我已经使用show modal对话框(JavaScript)实现了弹出窗口,但我仍然无法将父值转换为儿童表格!

i have implemented the popup using show modal dialog (JavaScript) but i still cant get the parent value into the child form!

此外,如果我有项目编号1和项目编号2,我希望两者都显示在确认框中!!

furthermore if i have item no 1 and item no 2 checked i want both to be displayed in the confirmation box!!

任何建议都会非常感激你!!!

any suggestion would be greatly appreciated thx you!!!

推荐答案

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.

这篇关于使用javascript showModalDialog将值从父窗体传递到子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆