javascript Jquery Onclick 将数据发送到新打开的窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12333560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:56:59  来源:igfitidea点击:

Jquery Onclick Send data to new opened window

phpjavascriptjquerypost

提问by Kaushil Rambhia

I know this is a common question but I can't find any answer. I have a PHP file which contains a link. When that link is clicked, I want to open another page in a new window, and on this newly opened page I want to get the ID attribute of the link clicked on the previous page.

我知道这是一个常见问题,但我找不到任何答案。我有一个包含链接的 PHP 文件。单击该链接时,我想在新窗口中打开另一个页面,在这个新打开的页面上,我想获取在上一页上单击的链接的 ID 属性。

Current page= 'patientreg.php'
Newpage to open in new window= 'patient_history.php'
Post data from current page to new loaded page.

当前页面= 'patientreg.php'
在新窗口中打开的新页面= 'patient_history.php'将
数据从当前页面发布到新加载的页面。

Current page Code:

当前页面代码:

HTML:

HTML:

<a id="37" onclick="phistory(this.id);" href="#">View history</a>  

JavaScript:

JavaScript:

function phistory(myVar) {  
    var myurl = '../main/patient_history.php';  
    $.post(myurl,{data:myVar},function(){});  
    window.open(myurl,'_blank');                
} 

New page code:

新页面代码:

$phid = $_REQUEST["data"];  
echo phid;

The problem is that it is not echoing the data on the next page, but I can see echo with the full new page in the Firebug response tab of the current page.

问题是它没有在下一页上回显数据,但是我可以在当前页面的 Firebug 响应选项卡中看到带有完整新页面的回显。

回答by andres descalzo

try this, so using a form to send data to your page, and you can have more verbose the anchor element:

试试这个,所以使用表单将数据发送到你的页面,你可以有更详细的锚元素:

HTML:

HTML:

<form id="myform" action="../main/patient_history.php" method="POST">
   <input type="hidden" name="data" id="data" />
</form> 


<a data-idref="37" class="see-history" href="#">View Histroy</a>

Javascript:

Javascript:

$(function() {

   $('.see-history').bind('click', function(event) {
      try { 
         window.open('about:blank', 'mynewwindow');   
         $('#myform').find('#data').val($(this).data('idref'));
         $('#myform').attr('target', 'mynewwindow').submit();
      } catch(e) {}

      return false;

   });

});

Edit

编辑

if you need to open a new window for each story, you can create a new window with the name of the element id:

如果您需要为每个故事打开一个新窗口,您可以使用元素 id 的名称创建一个新窗口:

   $('.see-history').bind('click', function(event) {
      var id = $(this).data('idref');  
      try { 
         window.open('about:blank', 'mynewwindow_' + id);   
         $('#myform').find('#data').val(id);
         $('#myform').attr('target', 'mynewwindow_' + id).submit();
      } catch(e) {}

      return false;

   });

回答by D. Strout

Try this instead. For your JavaScript onclickhandler:

试试这个。对于您的 JavaScriptonclick处理程序:

function phistory(myVar) {  
    var myurl = '../main/patient_history.php?data='+myVar;
    window.open(myurl);                
}

Then, in your PHP file:

然后,在您的 PHP 文件中:

$phid = $_REQUEST["data"];  
echo $phid;

Your problem is that you are misunderstanding the use of jQuery's .post()method. It simply retrieves the page (with the specified POST parameters) - it does not modify the page such that when you open it in a new window, it will have your specified ID parameter. I switched it so that it no longer uses jQuery's .post(), but simply uses the GET parameters by opening the new window with ?data=[YOUR-ID]. The PHP, you'll notice, stays mostly the same, but I changed the echo statement to include the $sign before the variable name. That might have been a typo just in the question, but if that's in your code, it's part of the problem.

您的问题是您误解了 jQuery.post()方法的使用。它只是检索页面(使用指定的 POST 参数)——它不会修改页面,因此当您在新窗口中打开它时,它将具有您指定的 ID 参数。我改变了它,使它不再使用 jQuery 的.post(),而是通过打开新窗口使用 GET 参数?data=[YOUR-ID]。您会注意到,PHP 基本保持不变,但我更改了 echo 语句以$在变量名之前包含符号。这可能只是问题中的一个错字,但如果这是在您的代码中,那么它就是问题的一部分。

Another possibility is to remove the ID from the query string completely, and instead store it as a global variable on the first page, then retrieve that with the window.openerproperty on the second page, like so:

另一种可能性是从查询字符串中完全删除 ID,而是将其作为全局变量存储在第一页上,然后使用window.opener第二页上的属性检索它,如下所示:

First page:

第一页:

var pid=0;
function phistory(myVar) {  
    pid=myVar;
    window.open("../main/patient_history.php");                
}

Second page (JavaScript):

第二页(JavaScript):

pid=window.opener["pid"];
alert(pid); //Just to check that it's working