DIV CSS 佈局教程網

JavaScript 父子視窗互相傳值的幾種做法
編輯:關於JavaScript     

很多 CMS 都有這類函式,DedeCMS 的「選擇相關文章」就是典型例子:按按鈕開一個清單視窗,在子視窗勾選後把值帶回父視窗的表單。

做法一:普通 window.open 視窗

父頁面的核心程式:

<script>
function SelectArcListA(fname){
var posLeft = 10;
var posTop = 10;
window.open("content_select_list.asp?f=" + fname + "&k=" + form1.keyword.value,
"selArcList",
"scrollbars=yes,resizable=yes,statebar=no,width=700,height=500,left=" + posLeft + ", top=" + posTop);
}
</script>
<input name="lsel" type="button" id="lsel" class="nbt" style="width:150px"
value="從已發布文檔中選取..." onClick="SelectArcListA('form1.xiangguanid');">

子視窗這邊,透過 window.opener 取得父視窗參照,就能直接寫入父頁面的欄位:

<SCRIPT language=javascript>
// 取得勾選的檔案 id
function getCheckboxItem()
{
var allSel = "";
if (document.form2.arcID.value) return document.form2.arcID.value;
for (i = 0; i < document.form2.arcID.length; i++)
{
if (document.form2.arcID[i].checked)
{
if (allSel == "") allSel = document.form2.arcID[i].value;
else allSel = allSel + "," + document.form2.arcID[i].value;
}
}
return allSel;
}
function selAll(){
for (i = 0; i < document.form2.arcID.length; i++)
if (!document.form2.arcID[i].checked) document.form2.arcID[i].checked = true;
}
function noSelAll(){
for (i = 0; i < document.form2.arcID.length; i++)
if (document.form2.arcID[i].checked) document.form2.arcID[i].checked = false;
}
function ReturnValue()
{
if (window.opener.document.form1.xiangguanid.value == ""){
window.opener.document.form1.xiangguanid.value = getCheckboxItem();
}
else{
window.opener.document.form1.xiangguanid.value += "," + getCheckboxItem();
}
alert("成功增加你選中的 ID,你可以繼續增加");
// window.close();
}
</SCRIPT>

頁面上的觸發連結:

<A class=inputbutx href="javascript:selAll()">全選</A>
<A class=inputbutx href="javascript:noSelAll()">取消</A>
<A class=inputbutx href="javascript:ReturnValue()">把選定值加到列表</A>

做法二:模態視窗 showModalDialog

父視窗(parentform.html):

<script language="javascript" type="text/javascript">
function doInput()
{
var win = window.showModalDialog("Childform.html", window,
"dialogWidth=500px;dialogHeight=300px;center=yes;status=no");
if (win != null)
{
document.getElementById("parentTextBox").value = win;
}
}
</script>
...
<input type="text" id="parentTextBox" />
<a href="javascript:doInput()">點這裡彈出子視窗</a>

子視窗(childform.html):

<script language="javascript" type="text/javascript">
// 從 dialogArguments 取得父視窗傳入的資料
document.getElementById("childTextBox").value =
window.dialogArguments.document.getElementById("parentTextBox").value;

function doPassToParent()
{
if (document.getElementById("childTextBox").value.length <= 0)
{
alert("請填寫數據");
return;
}
window.returnValue = document.getElementById("childTextBox").value;
window.close();
}
</script>

模態視窗的好處是流程清楚:showModalDialog 的第二個參數會成為子視窗的 dialogArguments,子視窗設定 window.returnValue 後關閉,父視窗的呼叫端就會拿到該值並繼續往下執行,不必依賴 opener 反向操作。

做法三:父子視窗互相呼叫函式

父視窗(Parent.html):

<script language="javascript" type="text/javascript">
function OpenWindow(){
window.open('son.html');
}
function setValue(m_strValue){
document.getElementById("txt_Value").value = m_strValue;
}
</script>
...
<input type="text" name="txt_Value" id="txt_Value" />
<input type="button" name="btn_ShowClose" id="btn_ShowClose" value="按鈕" onclick="OpenWindow();" />

子視窗(son.html):

<script language="javascript" type="text/javascript">
function CloseWind(){
opener.setValue("傳值到父窗體");   // 直接呼叫父視窗的方法
window.close();
}
</script>
...
<input type="button" name="btn_Close" id="btn_Close" value="按鈕" onclick="CloseWind();" />

這種做法比直接操作父視窗的 DOM 更好維護——父視窗把「怎麼寫入」封裝在 setValue() 裡,子視窗只負責呼叫,雙方耦合度低。三種做法的選擇原則:需要使用者不得關閉背景視窗時用模態視窗;要保留背景操作自由時用 window.open 搭配 opener。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved