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



我有一个带有链接的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>

我不明白为什么它不起作用。我的代码有问题吗?

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

假设你已经将jQuery加载到你的页面上(如果没有,你可以使用普通的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
}

最新更新