Javascript showModalDialog兩個窗體之間傳值
更新時間:2009年09月25日 14:51:35 作者:
前一篇文章Javascript怎么在兩個窗體之間傳值中講到了如何利用window.open()方法打開新窗體,并在兩個窗體之間傳值的方法。
Javascript 兩個窗體之間傳值實現(xiàn)代碼
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ù),類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
dialogHeight :對話框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默認的單位是em,而IE5中是px,為方便其見,在定義modal方式的對話框時,用px做單位。
dialogWidth: 對話框寬度。
dialogLeft: 離屏幕左的距離。
dialogTop: 離屏幕上的距離。
center: {yes | no | 1 | 0 }:窗口是否居中,默認yes,但仍可以指定高度和寬度。
help: {yes | no | 1 | 0 }:是否顯示幫助按鈕,默認yes。
resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改變大小。默認no。
status: {yes | no | 1 | 0 } [IE5+]:是否顯示狀態(tài)欄。默認為yes[ Modeless]或no[Modal]。
scroll:{ yes | no | 1 | 0 | on | off }:指明對話框是否顯示滾動條。默認為yes。
如:"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:
XML-Code:
<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>
同樣我們還可以傳遞對象,如:FatherPage.htm:
XML-Code:
<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:
<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:
XML-Code:
<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:
<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>
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ù),類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
dialogHeight :對話框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默認的單位是em,而IE5中是px,為方便其見,在定義modal方式的對話框時,用px做單位。
dialogWidth: 對話框寬度。
dialogLeft: 離屏幕左的距離。
dialogTop: 離屏幕上的距離。
center: {yes | no | 1 | 0 }:窗口是否居中,默認yes,但仍可以指定高度和寬度。
help: {yes | no | 1 | 0 }:是否顯示幫助按鈕,默認yes。
resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改變大小。默認no。
status: {yes | no | 1 | 0 } [IE5+]:是否顯示狀態(tài)欄。默認為yes[ Modeless]或no[Modal]。
scroll:{ yes | no | 1 | 0 | on | off }:指明對話框是否顯示滾動條。默認為yes。
如:"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:
XML-Code:
復制代碼 代碼如下:
<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>
同樣我們還可以傳遞對象,如:FatherPage.htm:
XML-Code:
復制代碼 代碼如下:
<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:
復制代碼 代碼如下:
<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:
XML-Code:
復制代碼 代碼如下:
<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:
復制代碼 代碼如下:
<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>
您可能感興趣的文章:
- javascript showModalDialog模態(tài)對話框使用說明
- js的window.showModalDialog及window.open用法實例分析
- JS中showModalDialog 的使用解析
- js showModalDialog參數(shù)的使用詳解
- javascript showModalDialog傳值與FireFox的window.open 父子窗口傳值示例
- js showModalDialog彈出窗口實例詳解
- javascript showModalDialog,open取得父窗口的方法
- JavaScript中window.showModalDialog()用法詳解
- javascript showModalDialog 多層模態(tài)窗口實現(xiàn)頁面提交及刷新的代碼
- JS對話框_JS模態(tài)對話框showModalDialog用法總結
- javascript showModalDialog 內(nèi)跳轉頁面的問題
- js showModalDialog 彈出對話框的簡單實例(子窗體)
- JS中showModalDialog關閉子窗口刷新主窗口用法詳解
相關文章
原生js實現(xiàn)仿window10系統(tǒng)日歷效果的實例
下面小編就為大家?guī)硪黄鷍s實現(xiàn)仿window10系統(tǒng)日歷效果的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10如何利用 JS 腳本實現(xiàn)網(wǎng)頁全自動秒殺搶購功能
這篇文章主要介紹了如何利用 JS 腳本實現(xiàn)網(wǎng)頁全自動秒殺搶購功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10JavaScript 面向對象程序設計詳解【類的創(chuàng)建、實例對象、構造函數(shù)、原型等】
這篇文章主要介紹了JavaScript 面向對象程序設計,結合具體實例形式詳細分析了JavaScript面向對象程序設計中類的創(chuàng)建、實例對象、構造函數(shù)、原型等相關概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-05-05