原文:劉武|Javascript怎么在兩個窗體之間傳值2-showModalDialog
前一篇文章Javascript怎么在兩個窗體之間傳值中講到了如何利用window.open()方法打開新窗體,并在兩個窗體之間傳值的方法。javascript中還有一個函數(shù)window.showModalDialog也可以打開一個新窗體,不過他打開的是一個模態(tài)窗口,那么如何在父窗體和子窗體之間傳值呢?我們先看該函數(shù)的定義:vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
參數(shù)說明:
sURL--必選參數(shù),類型:字符串。用來指定對話框要顯示的文檔的URL。
vArguments--可選參數(shù),類型:變體。用來向對話框傳遞參數(shù)。傳遞的參數(shù)類型不限,包括數(shù)組等。對話框通過window.dialogArguments來取得傳遞進來的參數(shù)。
sFeatures--可選參數(shù),類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
如:"dialogWidth=200px;dialogHeight=100px"
因此我們可以通過window.dialogArguments參數(shù)來在兩個窗體之間傳值
如下面兩個頁面:FatherPage.htm:
<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:
<body onload="Load()"> <script type="text/javascript"> function Load() { document.getElementById('txtInput').value=window.dialogArguments ; } </script> <input type="text" id="txtInput" /> </body>
上面只是傳遞簡單的字符串,我們還可以傳遞數(shù)組,如:FatherPage.htm:
<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:
<script type="text/javascript"> function Load() { document.getElementById('txtInput').value=window.dialogArguments[0] ; } </script>
同樣我們還可以傳遞對象,如:FatherPage.htm:
<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:
<script type="text/javascript"> function Load() { var obj = window.dialogArguments; document.getElementById('txtInput').value=obj.name ; } </script>
以上都是從父窗體向子窗體傳值,那么如何從子窗體向父窗體傳值呢 ?其實通過window.returnValue就可以獲取子窗體的值,window.returnValue與window.dialogArguments一樣,可以是任意變量,包括字符串,數(shù)組,對象等。如:FatherPage.html:
<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:
<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>
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答!! 點擊進入論壇