使用 javascript 单击按钮打开窗口作为模态对话框

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

open window on click of button as modal dialog using javascript

javascriptasp.nethtml

提问by John

I have to open a window when a button is clicked, as a modal dialog using JavaScript. I have used window.open, but it shows me two instances, the parent page and the pop-up page. When the pop-up page is open, then do not allow the user to click on the parent page.

单击按钮时,我必须打开一个窗口,作为使用 JavaScript 的模式对话框。我使用过window.open,但它显示了两个实例,父页面和弹出页面。当弹出页面打开时,不允许用户点击父页面。

function openWindow() {
  var w = 950;
  var h = 350;
  var t = 0;
  var l = 0;
  var scrollbars = 1;
  var modal = 'yes';

  var reportWindow = window.open("Search.aspx" + '', '', "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ',scrollbars=' + scrollbars + 'modal' + modal);
  reportWindow.focus();
}

回答by Matt

Your code line,

你的代码行,

reportWindow = window.open("Search.aspx" + '', '', "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ',scrollbars=' + scrollbars + 'modal' + modal);

is missing an '=' symbol after modal. It should be,

模态后缺少“=”符号。它应该是,

reportWindow = window.open("Search.aspx" + '', '', "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ',scrollbars=' + scrollbars + 'modal=' + modal);

To open window as dialog box,

要将窗口作为对话框打开,

 <html>
    <body>
    <script language="JavaScript">
    function openWindow() {
       if (window.showModalDialog) {
window.showModalDialog("http://example.com","name",
"dialogWidth:255px;dialogHeight:250px");
} else {
window.open('http://example.com','name',
'height=255,width=250,toolbar=no,directories=no,status=no, linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
}
    } 
    </script>
    <button onclick="openWindow();">Open window</button>
    </body>
    </html>

If the above code is not working, please use jQuery modal dialog. You can load any urls to dialog using an iframe.

如果上述代码不起作用,请使用 jQuery 模态对话框。您可以使用 iframe 将任何 url 加载到对话框中。

回答by Dominik Kirschenhofer

"window.open" will not work. This command always just opens a popup wich is not "always" on top of its parent.

“window.open”将不起作用。这个命令总是只打开一个弹出窗口,它并不“总是”在其父级之上。

2 Options:

2 选项:

  1. If you just want to let the user enter a search string you could use

    prompt("Please enter a search string:", "");
    
  2. If you have a complexe search form, you have to create a modal dialog for your own. There are frameworks which provide this functionality (google for "jQuery") or you create it for your own. Should not be hard, tell me if you need some help!

  1. 如果您只想让用户输入可以使用的搜索字符串

    prompt("Please enter a search string:", "");
    
  2. 如果您有一个复杂的搜索表单,您必须为自己创建一个模态对话框。有提供此功能的框架(谷歌为“jQuery”),或者您为自己创建它。应该不难,如果你需要帮助告诉我!