【问题标题】:Opening a new window with a button?用按钮打开一个新窗口?
【发布时间】:2012-06-17 03:40:49
【问题描述】:

我需要为我的打印打开一个新窗口,谁能告诉我该怎么做?

我的按钮点击代码:

我的 window.location 工作,但不能使用 window.open

$('.btnFromIndexPrint').click(function () {
    if (document.URL.indexOf('index') > -1) {
         // window.location = '../Print/' + $(this).attr('id');
         window.open = '../Print/' + $(this).attr('id');
    } else {
        //window.location = 'Contract/Print/' + $(this).attr('id'); //Redirect  from Index page to print action
        window.open = 'Contract/Print/' + $(this).attr('id');
    }

});

我的html:

我知道有一个叫做 target ="blank" id 的东西,但我认为它不会起作用。

<input type="button" value="Print" class="btnFromIndexPrint" id="@item.SalesContractId"/>

如何在新页面上打开重定向?

重要!!!!!!

return RedirectToAction("Print", new { id = contractInstance.SalesContractId });

【问题讨论】:

    标签: jquery asp.net-mvc new-window


    【解决方案1】:

    应该是:

    window.open(url_or_your_page);
    

    见:Example

    【讨论】:

    • 感谢这项工作:window.open('Contract/Print/' + $(this).attr('id'));
    • 如果适合您的需要,您可以将其标记为已接受的答案... :)
    • 我会的,你只能在 10 分钟后点击勾选 :) 你能在问题底部查看新的小问题吗?
    • 请查看底部的新小问题...有什么想法吗?
    • 哈哈抓住绿色勾号并运行:p,嘿,你不知道我怎样才能让 redirectToaction 显示在新页面上?
    【解决方案2】:

    你应该试试:

    window.open(url, [window name], "height=x,width=y");
    

    指定宽度/高度时,会在新窗口中打开。见window.open

    【讨论】:

      【解决方案3】:

      window.location 的语法是

      window.location = "url";
      

      例如:

      window.location ="http://www.mozilla.org";
      

      因此它在您的代码中运行良好。

      但是window.open() 的语法是

      window.open(URL, windowName[, windowFeatures])
      

      例如:

      window.open ("http://www.javascript-coder.com","mywindow","status=1");
      

      你的语法有问题。

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        首先,我希望你已经将上述代码包含在 jQuery 的 document.ready 函数中,或者将代码放在页面底部。这是因为如果指定的打印按钮尚未加载到 DOM 中,选择器 ($) 将找不到它,因此您的点击侦听器将不会附加到它。

        其次,window.open 是一个函数,不应像变量一样分配(您在上面已经完成了)。换句话说就是

        window.open( parameters ); //NOT window.open = value;
        

        请参阅下面的示例代码,这或多或少是对您上面的代码的更正和优化。

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Window.Open</title>
          <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
          <script type="text/javascript">
            //@Author: Prof. No Time
            $(document).ready(function(){
              $('.btnFromIndexPrint').click(function () {
                 var urlToOpen = '';
                 var docURL = document.URL;
        
                 if (docURL.indexOf('index') > -1) {
                    urlToOpen = '../Print/' + $(this).attr('id');
                 }
                 else {
                    urlToOpen = 'Contract/Print/' + $(this).attr('id');
                 }
        
                 //alert('urlToOpen > ' + urlToOpen);
                 if (urlToOpen != '') window.open(urlToOpen);
              });
           });
          </script>
         </head>
        
         <body>
            <input type="button" value="Print" class="btnFromIndexPrint" id="@item.SalesContractId"/>
         </body>
        </html>
        

        最后,我建议不要使用如上所示的此类 ID (@item.SalesContractId)。我想相信该值应该被服务器端处理替换的某个地方?

        希望这会有所帮助。

        【讨论】:

        • 关于主问题底部的新小问题。我想多解释一下。 window.open() 已经在新窗口/选项卡中打开了链接。请添加更多详细信息或提出新问题并将我指向链接。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        • 2013-09-22
        • 1970-01-01
        • 2019-04-12
        • 2011-06-06
        • 2017-06-14
        • 1970-01-01
        相关资源
        最近更新 更多