javascript 子窗體父窗體相互傳值方法
更新時間:2010年05月31日 22:07:38 作者:
javascript 子窗體父窗體相互傳值方法,一般都是用window.open函數(shù),下面腳本之家給出具體的代碼。
腳本之家精簡使用版本,一般情況好多cms都有一些這樣的函數(shù)。dedecms中的選擇相關(guān)文章也是用的這樣的函數(shù)。下面給出具體的代碼。
父頁面核心代碼:
<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="從已發(fā)布文檔中選取..." 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,你可以繼續(xù)增加");
//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>
一下是補(bǔ)充:
//模式窗體傳值
<!-- ====== 父窗體,我取名為parentform.html ==== -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>彈出窗口內(nèi)錄入數(shù)據(jù)確定后返回給父窗體--主窗體</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>在新彈出的窗體里輸入數(shù)據(jù),傳輸?shù)礁复绑w.
<br/>
<br/>
<br/>
<input type="text" id="parentTextBox" /> <a href="javascript:doInput()">點這里彈出子窗體</a>
</BODY>
</HTML>
<!-- ============= 父窗體代碼結(jié)束 ============= -->
<!-- ======= 子窗體:取名為childform.html ======= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title> 彈出窗口內(nèi)錄入數(shù)據(jù)確定后返回給父窗體--子窗體</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("請?zhí)顚憯?shù)據(jù)");
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>關(guān)閉
<input type="button" name="btn_Close" id="btn_Close" value="按鈕" onclick="CloseWind();"
/>
</label>
</form>
</body>
</html>
2.通過子窗體執(zhí)行的父窗體的setValue(m_strValue)來執(zhí)行賦值操作.
父頁面核心代碼:
復(fù)制代碼 代碼如下:
<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="從已發(fā)布文檔中選取..." onClick="SelectArcListA('form1.xiangguanid');">
子頁面的核心代碼:
復(fù)制代碼 代碼如下:
<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,你可以繼續(xù)增加");
//window.opener=true;
//window.close();
}
下面是html代碼,頁面中需要<input type="checkbox" name="arcID" value="<%=rs("id")%>">輸出選擇的id
復(fù)制代碼 代碼如下:
<A class=inputbutx
href="javascript:selAll()">全選</A> <A class=inputbutx
href="javascript:noSelAll()">取消</A> <A class=inputbutx
href="javascript:ReturnValue()">把選定值加到列表</A>
一下是補(bǔ)充:
//模式窗體傳值
<!-- ====== 父窗體,我取名為parentform.html ==== -->
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>彈出窗口內(nèi)錄入數(shù)據(jù)確定后返回給父窗體--主窗體</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>在新彈出的窗體里輸入數(shù)據(jù),傳輸?shù)礁复绑w.
<br/>
<br/>
<br/>
<input type="text" id="parentTextBox" /> <a href="javascript:doInput()">點這里彈出子窗體</a>
</BODY>
</HTML>
<!-- ============= 父窗體代碼結(jié)束 ============= -->
<!-- ======= 子窗體:取名為childform.html ======= -->
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title> 彈出窗口內(nèi)錄入數(shù)據(jù)確定后返回給父窗體--子窗體</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("請?zhí)顚憯?shù)據(jù)");
return;
}
window.returnValue = document.getElementById("childTextBox").value;
window.close();
}
</script>
//子窗體和父窗體傳值
1.新建兩個頁面 一個是 Parent.html
復(fù)制代碼 代碼如下:
<!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>
另一個是子窗體 :
復(fù)制代碼 代碼如下:
<!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>關(guān)閉
<input type="button" name="btn_Close" id="btn_Close" value="按鈕" onclick="CloseWind();"
/>
</label>
</form>
</body>
</html>
2.通過子窗體執(zhí)行的父窗體的setValue(m_strValue)來執(zhí)行賦值操作.
相關(guān)文章
JavaScript實現(xiàn)彈出子窗口并傳值給父窗口
這篇文章主要介紹了JavaScript實現(xiàn)彈出子窗口并傳值給父窗口,方法很簡單,這里推薦給大家,需要的朋友可以參考下2014-12-12js學(xué)習(xí)總結(jié)之dom2級事件基礎(chǔ)知識詳解
這篇文章主要為大家詳細(xì)介紹了js學(xué)習(xí)總結(jié)之dom2級事件基礎(chǔ)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07詳解JavaScript的閉包、IIFE、apply、函數(shù)與對象
本文主要對JavaScript的閉包、IIFE、apply、函數(shù)與對象進(jìn)行詳細(xì)介紹。有很好的參考價值,需要的朋友一起來看下吧2016-12-12Bootstrap基本組件學(xué)習(xí)筆記之分頁(12)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript與DropDownList 區(qū)別分析
大家都知道,.NET中一些Web服務(wù)器控件解析并編譯,最終被渲染的時候,其實是轉(zhuǎn)化成了普通的html控件。2010-01-01老生常談JavaScript面向?qū)ο蠡A(chǔ)與this指向問題
下面小編就為大家?guī)硪黄仙U凧avaScript面向?qū)ο蠡A(chǔ)與this指向問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10