父窗体得到子窗体的值(三种方式)

本文介绍了一个使用JavaScript实现的父窗口与子窗口间的数据交互示例。通过打开新窗口并利用window.showModalDialog及window.opener等方法进行数据传递,展示了不同窗口间的通信方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

testParent.jsp

 

<%@page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>

父窗体
</title>
<script type="text/javascript">
function setName(name){
 document.getElementById('name').value=name;
}

function showmodal(){
 var ret = window.showModalDialog("testChild.jsp",null,"dialogWidth:350px;dialogHeight:350px;help:no;status:no");
 document.getElementById('name').value=ret;
}
</script>
</head>
<body>
  <form action="" name="frm1">
  姓名:<input type="text" name="name" id="name"/>

    <input type="button" value="set" onclick="window.open('testChild.jsp')">
    <input type="button" value="setName" onclick="window.open('testChild.jsp')">
    <input type="button" value="showmodal()" onclick="showmodal();">
  </form>
</body>
</html>

 

 

 

 

testChild.jsp

 

<%@page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>

子窗体
</title>
<script type="text/javascript">
function putName(){
 window.opener.document.getElementById('name').value = document.getElementById('name').value;
 self.close();
}
function putName1(){
 var dateWeTi = document.getElementById("name").value;
 window.opener.setName(dateWeTi);
 window.close();
}

function putName2(){
 var dateWeTi = document.getElementById("name").value;
   window.returnValue = dateWeTi;
  window.close();
}
</script>
</head>
<body>
  <form action="" name="frm1">
  姓名:<input type="text" name="name" id="name"/>
  <input type="button" value="putName()" onclick="putName();">
  <input type="button" value="putName1()" onclick="putName1();">
  <input type="button" value="putName2()" onclick="putName2();">
  </form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值