使用javascript在弹出窗口中填充父窗口中的值

时间:2015-12-07 17:21:33

标签: javascript jsp

我有一个带链接的jsp页面。

<a href="PopUp.jsp" target="_blank" id="popUp">View</a>

单击链接后,PopUp.jsp将在另一个窗口中打开。

我想用第一个jsp中的值填充此窗口。

实施例: Parent.jsp

<input type="text" id="name"/>
<input type="text" id="city"/>
<a href="PopUp.jsp" target="_blank" id="popUp">View</a>

PopUp.jsp

<script>
    function setThis(){
        document.getElementById("pname").value=document.getElementById("name").value;
        document.getElementById("pcity").value=document.getElementById("city").value;
    }
</script>
<body onload="setThis();">
<input type="text" id="pname"/>
<input type="text" id="pcity"/>
</body>

我无法弄清楚它为什么不起作用。我的代码有问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用window.open方法并使用其返回的对象来访问新窗口实例。返回的对象可用于访问新窗口的属性和方法,前提是它符合Same origin策略安全性要求。

假设您已将jQuery加载到您的页面(如果没有,您可以使用vanilla javascript将点击事件连接到您的代码。使用onclick事件)

$(function(){

  $("#popUp").click(function(e){

   e.preventDefault();

   var url="url to the new page here";

   var newWindow = window.open(url);

   var pname="read from this page";
   var pcity="read from this page";

   newWindow.setThis(pname,pcity);

  });

});

更新您的setThis以接受这些值

function setThis(pname,pcity)
{
  .// do something
}
相关问题