JS父窗體與子窗體調用
【總結】
1.使用window.showModalDialog方法
父窗體:var retrunValue=window.showModalDialog(...)方法獲得子窗體的值
子窗體:
a.獲得父窗體控件的值: window.dialogArguments.getElementById(...)
b.子窗體返回值給父窗體: window.returnValue=....;window.close();
2.使用window.open方法
opener.**** 為調用父窗體
例如:opener.location.reload(); 父窗體刷新
opener.refresh(); 調用父窗體refresh() js方法
opener.getElementById("btnAdd").click(); 調用父窗體執行按鈕操作
//.......................................................使用window.showModalDialog方法....................................................................
<!-- ====== 父窗體,我取名為parentform.html ==== -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>彈出窗口內錄入數據確定后返回給父窗體--主窗體</title>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="qiujy">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<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>
</head>
<body>在新彈出的窗體里輸入數據,傳輸到父窗體.
<br/>
<br/>
<br/>
<input type="text" id="parentTextBox" /> <a href="javascript:doInput()">點這里彈出子窗體</a>
</BODY>
</HTML>
<!-- ============= 父窗體代碼結束 ============= -->
<!-- ======= 子窗體:取名為childform.html ======= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title> 彈出窗口內錄入數據確定后返回給父窗體--子窗體</title>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="qiujy">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<base target="_self">
</head>
<body topmargin="0" leftmargin="0" scroll="no">
</br>
<input type="text" id="childTextBox"/>
</br></br>
<a href="javascript:doPassToParent()">點這里返回</a>
</BODY>
</HTML>
<script language="javascript" type="text/javascript">
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>
//.......................................................使用window.open方法....................................................................
1.新建兩個頁面 一個是 Parent.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>父窗體</title>
<script language="javascript" type="text/javascript">
function OpenWindow(){
window.open('son.html');
}
function setValue(m_strValue){
document.getElementById("txt_Value").value = m_strValue;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="text" name="txt_Value" id="txt_Value" />
</label>
<label>
<input type="button" name="btn_ShowClose" id="btn_ShowClose" value="按鈕" onclick="OpenWindow();" />
</label>
</form>
</body>
</html>
另一個是子窗體 :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>子窗體</title>
<script language="javascript" type="text/javascript" >
function CloseWind(){
opener.setValue("傳值到父窗體");
window.close();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>關閉
<input type="button" name="btn_Close" id="btn_Close" value="按鈕" onclick="CloseWind();"
/>
</label>
</form>
</body>
</html>
2.通過子窗體執行的父窗體的setValue(m_strValue)來執行賦值操作.