Javascript ShowModalDialog Two form-pass values _javascript tips

Source: Internet
Author: User
Javascript two form pass value implementation code
There's also a function in JavaScript window.showmodaldialog can also open a new form, but he opens a modal window, so how do you pass values between the parent form and the subform? Let's look at the definition of the function: Vreturnvalue = window.showModalDialog (sURL [, varguments] [, Sfeatures])
Parameter description:
surl--required parameter, type: String. The URL used to specify the document to display in the dialog box.
varguments--optional parameter, type: Variant. Used to pass parameters to the dialog box. The parameter types passed are not limited, including arrays, and so on. The dialog box is window.dialogarguments to get the parameters passed in.
sfeatures--optional parameter, type: String. Used to describe information such as the appearance of a dialog box, you can use one or more of the following, with a semicolon ";" Separated.
Dialogheight: Dialog height, not less than 100px,ie4 dialogheight and dialogwidth The default unit is EM, and IE5 is PX, in order to facilitate its see, in the Definition of modal dialog box, with PX to do units.
Dialogwidth: Dialog box width.
Dialogleft: The distance from the left of the screen.
Dialogtop: The distance from the screen.
Center: {yes | no | 1 | 0}: The window is centered, the default yes, but the height and width can still be specified.
Help: {yes | no | 1 | 0}: Do you want to show the assistance button, default yes.
Resizable: {yes | no | 1 | 0} [ie5+]: can be changed size. Default No.
Status: {yes | no | 1 | 0} [ie5+]: Whether the status bar is displayed. Default is yes[Modeless] or no[modal].
scroll:{Yes | no | 1 | 0 | on | off}: Indicates whether the dialog box displays scroll bars. The default is yes.
such as: "dialogwidth=200px;dialogheight=100px"
So we can pass the values between two forms through the window.dialogarguments argument.
such as the following two pages: fatherpage.htm:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openchildwindow ()
{
window.showModalDialog (' childpage.htm ', document.getElementById (' Txtinput '). Value);
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Openchild" onclick= "Openchildwindow ()"/>

Childpage.htm:
Copy Code code as follows:

<body onload= "Load ()" >
<script type= "Text/javascript" >
function Load ()
{
document.getElementById (' Txtinput '). value=window.dialogarguments;
}
</script>
<input type= "text" id= "Txtinput"/>
</body>

Just passing a simple string, we can also pass an array, such as: fatherpage.htm:
Xml-code:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openchildwindow ()
{
var args = new Array ();
Args[0] = document.getElementById (' Txtinput '). Value;
window.showModalDialog (' childpage.htm ', args);
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Openchild" onclick= "Openchildwindow ()"/>childpage.htm:
Xml-code:
<script type= "Text/javascript" >
function Load ()
{
document.getElementById (' Txtinput '). Value=window.dialogarguments[0];
}
</script>

We can also pass objects, such as: fatherpage.htm:
Xml-code:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openchildwindow ()
{
var obj = new Object ();
Obj.name = document.getElementById (' Txtinput '). Value;
window.showModalDialog (' childpage.htm ', obj);
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Openchild" onclick= "Openchildwindow ()"/>

Childpage.html:
Xml-code:
Copy Code code as follows:

<script type= "Text/javascript" >
function Load ()
{
var obj = window.dialogarguments;
document.getElementById (' Txtinput '). Value=obj.name;
}
</script>

All of these are values from the parent form to the subform, so how do you pass values from the subform to the parent form? In fact, through Window.returnvalue can get the value of the subform, Window.returnvalue and window.dialogarguments, can be arbitrary variables, including strings, arrays, objects, and so on. such as: fatherpage.html:
Xml-code:
Copy Code code as follows:

<script type= "Text/javascript" >
function Openchildwindow ()
{
var obj = new Object ();
Obj.name = document.getElementById (' Txtinput '). Value;
var result = window.showModalDialog (' childpage.htm ', obj);
document.getElementById (' Txtinput '). Value = Result.name;
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Openchild" onclick= "Openchildwindow ()"/>

Childpage.html:
Xml-code:
Copy Code code as follows:

<body onload= "Load ()" >
<script type= "Text/javascript" >
function Load ()
{
var obj = window.dialogarguments;
document.getElementById (' Txtinput '). Value=obj.name;
}
function SetValue ()
{
var obj = new Object ();
Obj.name = document.getElementById (' Txtinput '). Value;
Window.returnvalue = obj;
Window.close ();
}
</script>
<input type= "text" id= "Txtinput"/>
<input type= "button" value= "Setfather" onclick= "SetValue ()"/>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.