DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript 子窗體父窗體相互傳值方法
javascript 子窗體父窗體相互傳值方法
編輯:關於JavaScript     
精簡使用版本,一般情況好多cms都有一些這樣的函數。dedecms中的選擇相關文章也是用的這樣的函數。下面給出具體的代碼。
父頁面核心代碼:
復制代碼 代碼如下:
<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" onChange="" value="從已發布文檔中選取..." onClick="SelectArcListA('form1.xiangguanid');">

子頁面的核心代碼:
復制代碼 代碼如下:
<SCRIPT language=javascript>
//獲得選中文件的文件名
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.opener=true;
//window.close();
}

下面是html代碼,頁面中需要<input type="checkbox" name="arcID" value="<%=rs("id")%>">輸出選擇的id
復制代碼 代碼如下:
<A class=inputbutx
href="javascript:selAll()">全選</A>   <A class=inputbutx
href="javascript:noSelAll()">取消</A>   <A class=inputbutx
href="javascript:ReturnValue()">把選定值加到列表</A>

一下是補充:
//模式窗體傳值

<!-- ====== 父窗體,我取名為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="jb 51.net">
<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>

//子窗體和父窗體傳值

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)來執行賦值操作.
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved