弹出窗口关闭后如何触发父窗口事件 [转帖]

    一,最简单的就是同一个网页里的表单的数据传递。

    举个实例,一个网页上有两个表单,每个表单里一个文本框,一个按钮。点按钮互相对操作对方的文本框的值。我们举的例子是把一个文本框付给另一个文本框。具体的HTML代码如下:


<html>
<head>
<title>Untitled Document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>

<form name="form1" method="post" action="">
  <input type="text" name="textfield">
  <input type="button" name="Submit" value="1---------&gt;2" onClick="ok()">
</form>

<form name="form2" method="post" action="">
  <input type="text" name="textfield2">
  <input type="button" name="Submit" value="2-----&gt;1" onClick="ok1()">
</form>

</body>
</html>




  以上为HTMl的代码,大家可能注意到了onclik的代码了,有两个函数,接下来就是javascript的代码了:


<script language="javascript">
function ok()
{
  document.form2.textfield2.value=document.form1.textfield.value;
}
function ok1()
{
document.form1.textfield.value=document.form2.textfield2.value;
}
</script>



  二,第二种是两个窗口之间的表单的文本框之间数据传递。

  其实这个可以在原来的基础上进行一些扩展就可以了。关于如何创建弹出窗口,窗体里的表单的代码, 在这里就不多说了,现在在这里说一下如何操作父窗口的表单里的文本框的数据。具体代码如下:


<script language="javascript">
function ok()
{
  opener.document.form2.textfield2.value=document.form1.textfield.value
}
</script>





  三,第三种就是框架网页之间的表单的文本框之间数据传递.

        注意的地方是框架的写法:


<frameset cols="505,505">
  <frame src="test.htm" name="leftr" id="leftr">//定义框架的名称
  <frame src="test2.htm" id="right" name="right">
</frameset>
<noframes> <body>

</body> </noframes>



具体的实现代码如下:


<script language="javascript">
function ok()
{
  parent.leftr.document.form2.textfield2.value=document.form1.textfield.value
}
</script>


    这三种窗口之间的文本框数值互相操作的简单方法就实现了,其它需要注意的就是他们之间的关系。

弹出窗口给父窗口传值并执行父窗口中的事件:

父窗口中弹出窗口脚本:
function GetWinModalInfo()
        {
            var strReturn = "height=520, width=820, toolbar=no , menubar=no, scrollbars=no, resizable=no, location=no, status=no";
            return strReturn;
        }
        var valueObj;  
  function OpenSelectWin(url)
  {
      var strWinModalInfo = GetWinModalInfo();
   valueObj = window.open(url,"newWindow",strWinModalInfo);
  }

弹出窗口传回值脚本:
<script language='javascript'>window.opener.GetSelectedVale(" + selValue + ");window.close();</script>

父窗口中接收值并执行相应事件脚本:
function GetSelectedVale(val)
  {     
      if(val != "")
   {
       document.forms[0].<%= txt.ClientID %>.value = val;
    document.forms[0].<%= btn.ClientID %>.click();
   }
  }

转自:

http://topic.csdn.net/u/20081026/21/870ebc90-3c16-4ca8-bca5-4088ce2760a9.html

http://www.cnblogs.com/ezhubin/news/2007/05/31/766944.html

原文地址:https://www.cnblogs.com/liangwei389/p/1330384.html