子窗口赋值到父窗口

代码如下

复制代码
<script>
function openWin(str) {
    window.open(siteurl+"popup/"+str, null,'width=800,height=500'); // 打开窗口

</script> 
<input type="text" id="title" name="picPath" value="<?php if(isset($pic)) {echo $pic['Path'];}?>" /> 
<a href="javascript:;" onclick="openWin('searchPic');">图片</a>

子窗口:

 代码如下 复制代码

<html>
    <head>
        <title>图片搜索</title>
    </head>
    <body>
        <script>
            function getValue() {
                window.opener.document.getElementById('title').value = document.getElementById('picPath').value // 赋值
                window.close(); // 关闭窗口
            }
        </script>
        <input type="text" id="picPath" />
        <input type="button" value="确定" onclick="getValue()" />
    </body>

1、子窗口与父窗口间通信

(1) 使用window.open()创建的窗口与父窗口通信

可以在子窗口页面中通过window.opener来获取父窗口对象,获取之后子窗口便可以对父窗口执行刷新,传值等操作。

如:

 代码如下 复制代码

window.opener.location.reload(); //子窗口刷新父窗口
window.opener.location.href //获取父窗口href
window.opener.locaiton.pathname //获取父窗口路径名

//刷新父页面
window.location.href=window.location.href ; //重新定位父页面
window.location.reload;

(2) 模态窗口与父窗口通信

通过使用showModelDialog(),及showModelessDialog()方法创建的子窗口想与父窗口通信时,不能通过window.opener

来获取父窗口对象。要实现通信,必须在创建模态子窗口时向子窗口传入父窗口对象。

实现方式为:

在父窗口中:

 代码如下 复制代码

var newWin=window.showModelDialog(url,window,'');
newWin.open();

此时参数window即是父窗口对象

例子

A页面<script type="text/javascript">

 代码如下 复制代码
        function popUp(url) {  
            objSubWin = window.open(url, "Popup", "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=300,height=80");  
            objSubWin.focus();  
        }  
  
        function SetValue(val) {  
            var amount = document.getElementById('<% = TextBoxAmount.ClientID %>');  
         amount.value = val;  
     }  
  
    </script>  
[csharp]  
<asp:TextBox ID="TextBoxAmount" runat="server" Enabled="false"></asp:TextBox>  
            <asp:Button ID="Button1" runat="server" Text="Call child window" OnClientClick="popUp('PageB.aspx')" />  
 
B页面:
<script type="text/javascript">
        function isNumeric(keyCode) {
            return ((keyCode >= 48 && keyCode <= 57) || keyCode == 8)
        }
 
        function calc() {
            if (window.opener != null && !window.opener.closed) {
                var qty = document.getElementById('<% = TextBoxqty.ClientID %>');
                var price = document.getElementById('<% = TextBoxPrice.ClientID %>');
 
                window.opener.SetValue(parseInt(qty.value) * parseInt(price.value));
            }
        }
    </script>
数量<asp:TextBox ID="TextBoxqty" runat="server" onkeydown="return isNumeric(event.keyCode);" onpaste="return false;" Width="50"></asp:TextBox>
            * 单价<asp:TextBox ID="TextBoxPrice" runat="server" onkeydown="return isNumeric(event.keyCode);" onpaste="return false;" Width="50"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Calculate" OnClientClick="calc()" />

在子窗口中:

需首先获取父窗口对象,然后才能使用父窗口对象。由于父窗口对象是在创建
子窗口时通过传入参数的方式传入的,因此,在子窗口中也只能通过获取窗口参数的方式获取父窗口对象。获取方式如下:

 代码如下 复制代码
var parent=widnow.dialogArguments;

变量parent便是父窗口对象。

例如:

 代码如下 复制代码

//通过子窗口提交父窗口中的表单:form1,提交后执行查询操作
var parent=window.dialogArguments;
parent.document.form1.action="QueryInfor.jsp";
parent.submit();

//刷新父页面
var parent=window.dialogArguments;
parent.location.reload();

//从子窗口传值到父窗口
要实现在模态子窗口中传值到父窗口,需要使用window.returnValue完成

实现方法如下:

在子窗口中:

 代码如下 复制代码

//获取父窗口某字段值,对该值加一后返回父窗口
var parent=window.dialogArguments;
var x=parent.docuement.getElementById("age").value;
x=x+1;

//传回x值
window.returnValue=x;

在父窗口中:

 代码如下 复制代码

//获取来自子窗口的值
var newWin=window.showModelDialog(url,window,'');
if(newWin!=null)
document.getElementByIdx_x("age").value=newWin;

//在子窗口中设置父窗口的值
在子窗口中向父窗口中传入值似乎没有直接设置父窗口中的值来得明了。直接设置父窗口中元素的值显得要更灵活一些,不过具体使用哪种方法要根据实际情况和已有的实现方式而定,因为如果使用了不切实际的方法不仅降低开发效率,也降低了执行效率,往往也会造成不优雅的实现方式和代码风格。

子窗口设置父窗口的值使用方法如下:

子窗口中:

 代码如下 复制代码

var parent=window.dialogArguments;
var x=parent.document.getElementByIdx_x("age").value;

x=x+1;
//设置父窗口中age属性值

parent.document.getElementByIdx_x("age").value=x;

子窗口和父窗口交互的内容,是把子窗口的信息传递给父窗口,并且关闭自己等等,或者是父窗口把自己的信息传递给子窗口等等。

1。父窗口传递信息给子窗口

看代码实例:

 代码如下 复制代码

<script language=javascript>

function outPut()
{
//获取父窗口的文本信息赋值给text
var text = document.abc.text.value;
//打开子窗口,并且把操作句柄赋值给win变量,以下所有操作都是针对win对象的
var win = window.open(”",”mywin”, “menubar=no,width=400,height=100,resizeable=yes”);
//输出基本信息
win.document.writeln(”<title>输出结果</title>”);
win.document.writeln(”你的信息是:<p>”);
//输出从父窗口获取的信息
win.document.writeln(text);
win.document.close();
win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
//调用上面的函数
<input type=button value=提交 onClick=”outPut()”>

</form>

2。子窗口传递参数给父窗口

我们对上面的代码进行改造:

 代码如下 复制代码

<script language=javascript>

function outPut()
{
var text = document.abc.text.value;
var win = window.open(”",”mywin”, “menubar=no,width=400,height=100,resizeable=yes”);
win.document.writeln(”<title>输出结果</title>”);
win.document.writeln(”你的信息是:<p>”);
win.document.writeln(text);
win.document.writeln(”<input type=text name=child value=子窗口信息>”);

//对子窗口本身操作,使用self对象,对父窗口操作使用opener对象,这是关键
//把子窗口中表单的值回传给父窗口,取代父窗口表单以前的值,然后关闭子窗口
win.document.writeln(”<input type=button value=关闭自己 onClick= window.opener.abc.text.value=self.child.value;self.close() >”);
//可以控制关闭父窗口
win.document.writeln(”<input type=button value=关闭父窗口 onClick= window.opener.opener=null;window.opener.close() >”);
//刷新父窗口
win.document.writeln(”<input type=button value=刷新父窗口 onClick= window.opener.location.reload() >”);

win.document.close();
win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
<input type=button value=提交 onClick=”outPut()”>

</form>

3。不是同页面的子窗口和父窗口交互

假设我们涉及到外部程序,比如php、asp等等,那么我们处理的可能是两个页面,比如,上传功能,我们就是需要打开一个新页面,然后再把新页面中的值传递给父页面。

 代码如下 复制代码

局部代码实例:

<input type=”input” value=”" name=”input_tag” id = “input_tag” onKeyUp=”clearPreTagStyle()” size=”40″>
<input type=”hidden” value=”0″ name=”tagid” id=”tagid”>
<input type=”button” value=”标签” onclick=”popUpWindow( tag.php?tag= +escape(document.tryst_form.input_tag.value))”>

以上是父窗口的部分代码,里面的popUpWindow是封装好的window.open函数,所以理解面面的tag.php是另外一个页面就可以,我们需要把当前表单中的值提交给tag.php页面去处理。

tag.php部分代码:

查询标签结果:

 代码如下 复制代码

<a href=”#” name=”tag_1″>生活</a><a href=”#” onclick=”opener.document.tryst_form.input_tag.value = document.tag_1.innerHTML”>加入该标签</a>

<a href=”#” name=”tag_2″>生活秀</a><a href=”#” onclick=”opener.document.tryst_form.input_tag.value = document.tag_2.innerHTML”>加入该标签</a>

这个就是我们的子窗口,g:w7垠件的专e 育,网E我们要把tag_1和tag_2返回到子窗口中,虽然他们不是同一个页面。这里有个知识点,就是我们如何获取连接中的值,我们使用 innerHTML属性:document.tag_2.innerHTML 这个就是我们获取了tag_2的值“生活秀”,我们也能使用其他方法获取,比如:document.all.tag_2.innerHTML,

或者this.innerHTML就是指本身的链接的值。

访问父窗口也是使用opener对象来处理:opener.document.tryst_form.input_tag.value,

就能够改变父窗口的值。

JS子父窗口互相取值赋值详解介绍的更多相关文章

  1. JS子父窗口互相操作取值赋值的方法介绍

    $("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementById ...

  2. cin 对象取值过程详解

    突然又空,鉴于对cin对象的去值机制还不是很了解,就探究一番,并且记下来,方便以后复习. #include <iostream> int main(void) { using namesp ...

  3. loadrunner 参数化取值方式详解

    参数化对话框中与参数取值方式有关的区域如下: 改变参数化的取值方式,关键在于Select next row和Update value on这两个选项. Select next row包括以下选项: S ...

  4. mysql数据库TINYINT取值范围详解

    分享下mysql中TINYINT的取值范围,很基础的一些内容. 在MySQL的数据类型中,Tinyint的取值范围是:带符号的范围是-128到127.无符号的范围是0到255(见官方<MySQL ...

  5. js表单快速取值/赋值 快速生成下拉框

    1.表单取值/赋值公共方法 //表单序列化:文本框的name字段和数据源一致<form id="myForm" onsubmit="return false;&qu ...

  6. JQuery关于span标签的取值赋值

    span取值赋值方法有别于一般的页面元素.JQ://赋值$("#spanid").html("hello world") //取值$("#spanid ...

  7. Dynamics 365-表单元素取值/赋值

    取值/赋值 参考: 山人丶 提示: 查找类型赋值时需指定目标实体,记录名称及id值 时间和日期类型赋值时需赋值Date类型 //获取new_name的值(单行文本) Xrm.Page.getAttri ...

  8. Jquery 关于span标签的取值赋值用法

    span是最简单的容器,可以当作一个形式标签,其取值赋值方法有别于一般的页面元素. //赋值 $("#spanid").html(value) //取值 $("#span ...

  9. Jquery操作下拉框(DropDownList)实现取值赋值

    Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...

随机推荐

  1. Beeline known issues

    If you use nohup myscript.sh , You beeline scripts may not work, Pay attention to this in your job.

  2. FPGA 设计技巧

    1.  资源共享的应用限制在同一个module里 这样 综合工具才能最大限度地发挥其资源共 享综合作用 2.  尽可能将Critical path上所有相关逻辑放在同一个module里 这样 综合工具 ...

  3. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  4. 日期选择器:jquery datepicker的使用

    helloweba.com 作者:月光光 时间:2012-04-08 21:05 标签: jquery  datepicker  jquery ui     在jquery ui中,提供了一个非常实用 ...

  5. 关于Yii2中count方法的使用

    统计文章与分类中间表中c_id的数目,也就是category表中total字段的值 原生SQL语句:select count(c_id) from article_category where c_i ...

  6. jquery堆栈与队列

    期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待期待

  7. vrrp两用

    早上想了想vrrp的使用,1,网关冗余 2,服务器热备 思想稍微有点不一样.主要在于监控口 服务器的话有心跳线,用户同步一些配置和迁移一些服务.达到热备的目的.:牵涉到四个优先级:建议这样排序: 主机 ...

  8. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. 自己写的AES和RSA加密解密工具

    package com.sdyy.common.utils; import java.security.Key; import java.security.KeyFactory; import jav ...

  10. JS面向对象的几种写法

    JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...