showModalDialog在父窗體和子窗體之間傳值


Javascript中有一個函數window.showModalDialog可以打開一個新窗體,是一個模態窗口,那么如何在父窗體和子窗體之間傳值呢?我們先看該函數的定義:vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
參數說明:

sURL--必選參數,類型:字符串。用來指定對話框要顯示的文檔的URL。
vArguments--可選參數,類型:變體。用來向對話框傳遞參數。傳遞的參數類型不限,包括數組等。對話框通過window.dialogArguments來取得傳遞進來的參數。
sFeatures--可選參數,類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
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+]:是否顯示狀態欄。默認為yes[ Modeless]或no[Modal]。
scroll:{ yes | no | 1 | 0 | on | off }:指明對話框是否顯示滾動條。默認為yes。


如:"dialogWidth=200px;dialogHeight=100px"
因此我們可以通過window.dialogArguments參數來在兩個窗體之間傳值
如下面兩個頁面: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>

上面只是傳遞簡單的字符串,我們還可以傳遞數組,如: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一樣,可以是任意變量,包括字符串,數組,對象等。如: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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM