jQuery 如何在window.open方法中传递参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16276789/
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-08-26 16:51:02  来源:igfitidea点击:

How to pass parameter in window.open method?

javascriptjqueryasp.nethtmlasp.net-mvc

提问by Naruto

I have the following method in JavaScript:

我在 JavaScript 中有以下方法:

var string1 = "testing";
var date = "10/10/2012";

var win = window.open(BuildUrl(("report", "report"), "myreports",  
           "toolbar=0,statusbar=0,scrollbars=1,resizable=1,location=0");

function BuildUrl(controllerName, actionName) {        
    var TimeStamp = Number(new Date()); 
    var win = window.location;
    return win.protocol + "//" + win.host + "/" + controllerName + "/" + actionName + '?_=' + TimeStamp ;  
}

Method in C# controller looks like this:

C# 控制器中的方法如下所示:

public ActionResult report()
{               
    return View();
}

Now I need to pass parameters like name and date when the URL is accessed to the C# method. How can I do that?

现在我需要在访问 URL 时将名称和日期等参数传递给 C# 方法。我怎样才能做到这一点?

回答by Vimal Stan

Append the parameters to the URL in the following format:

按以下格式将参数附加到 URL:

<url>?param1=value1&param2=value...

If you need to pass an array of values then use the same name for the parameter

如果您需要传递一组值,则对参数使用相同的名称

<url>?arr=value1&arr=value2...

So your URL would look like

所以你的网址看起来像

domain.com/Controller/Report?name=xyz&date=20

domain.com/Controller/Report?name=xyz&date=20



Update:

更新:

To receive them in the Action, declare the parameters being passed to the Action.

要在 Action 中接收它们,请声明传递给 Action 的参数。

public ActionResult Report(string name, DateTime date)
{
  ...
}

Read up on Model Bindingin ASP.NET MVC

阅读ASP.NET MVC 中的模型绑定

回答by BlueMan

I found a better way to pass parameters to the popup window and even to retrieve parameters from it :

我找到了一种将参数传递给弹出窗口甚至从中检索参数的更好方法:

In the main page :

在主页面:

var popupwindow;
var sharedObject = {};

function openPopupWindow()
{
   // Define the datas you want to pass
   sharedObject.var1 = 
   sharedObject.var2 = 
   ...

   // Open the popup window
   window.open(URL_OF_POPUP_WINDOW, NAME_OF_POPUP_WINDOW, POPUP_WINDOW_STYLE_PROPERTIES);
   if (window.focus) { popupwindow.focus(); }
}

function closePopupWindow()
{
    popupwindow.close();

    // Retrieve the datas from the popup window
    = sharedObject.var1;
    = sharedObject.var2;
    ...
}

In the popup window :

在弹出窗口中:

var sharedObject = window.opener.sharedObject;

// function you have to to call to close the popup window
function myclose()
{
    //Define the parameters you want to pass to the main calling window
    sharedObject.var1 = 
    sharedObject.var2 = 
    ...
    window.opener.closePopupWindow();
}

That's it !

就是这样 !

And this is very convenient because:

这非常方便,因为:

  • You have not to set parameters in the URL of the popup window.
  • No form to define
  • You can use illimited parameters even objects.
  • Bi-directionnal : you can pass parameters AND, if you want you, can retreive new parameters.
  • Very easy to implement.
  • 您不必在弹出窗口的 URL 中设置参数。
  • 没有定义的形式
  • 您可以使用无限参数甚至对象。
  • Bi-directionnal :您可以传递参数,如果需要,还可以检索新参数。
  • 非常容易实施。

Have Fun!

玩得开心!