jQuery 打开一个新的弹出窗口并将数据发布到其中

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

Open a new popup window and post data to it

jquerypopuphttp-post

提问by Pramod

I'm using jQuery to open a popup and I'd like to send it data using the post method when it opens. Can anyone help me, thanks in advance.

我正在使用 jQuery 打开一个弹出窗口,我想在它打开时使用 post 方法向它发送数据。任何人都可以帮助我,在此先感谢。

I am currently passing the data using the get method so the data is a part in url, but I don't want the data to be visible in the url.

我目前正在使用 get 方法传递数据,因此数据是 url 的一部分,但我不希望数据在 url 中可见。

function openWindow(){

    var name = $('#name').val();

    var url = 'popup_window.php?name='+name;
    window.open(
        url,
        'popUpWindow',
        'height=400,     \
         width=650,      \
         left=300,       \
         top=100,        \
         resizable=yes,  \
         scrollbars=yes, \
         toolbar=yes,    \
         menubar=no,     \
         location=no,    \
         directories=no, \
         status=yes');
}

回答by Barmar

This is based on the answer in How to open popup and populate it with data from parent window?

这是基于如何打开弹出窗口并使用父窗口中的数据填充它中的答案

var newpage;
function openWindow() {
    $.post('popup_window.php', {name: $('#name').val()}, function(result) {
        newpage = result;
        window.open('Popup.html', 'popUpWindow','height=400, width=650, left=300, top=100, resizable=yes, scrollbars=yes, toolbar=yes, menubar=no, location=no, directories=no, status=yes');
    });
}

Popup.html should contain:

Popup.html 应包含:

<script type="text/javascript">
    if(window.opener && !window.opener.closed) {
        document.write(window.opener.newpage);
    }
</script>