Jquery -打开一个包含当前页面内容的新窗口


Jquery - Open a new window with content from the current page

我试图创建一个'打印'按钮打开一个新窗口,并在其中显示一个PHP变量。

下面的代码是通过PHP脚本循环多次有票,但是我似乎不能得到正确的数字显示当窗口打开(打印链接中显示的数字是正确的-但当新窗口打开时,它是不正确的)。

<script type='text/javascript'>
jQuery(function($) {
$('a.new-window').click(function(){
var recipe =  window.open('','PrintWindow','width=600,height=600');
var html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + $('<div />').append($('#ticket').clone()).html() + '</div></body></html>';
recipe.document.open();
recipe.document.write(html);
recipe.document.close();
return false;
});
});
</script>
<a href="" class="new-window">Print <?php echo $EM_Booking->get_spaces() ?></a> 
<div style="display:none;">
<div id="ticket"><?php echo $EM_Booking->get_spaces() ?>
</div>
</div>

为什么不试试这样:

<a href="#" class="new-window">Print <?php echo $EM_Booking->get_spaces() ?>
    <div class="ticket" style="display:none">
        <?php echo $EM_Booking->get_spaces() ?>
    </div>
</a> 

<script>
jQuery(function ($) {
    $('a.new-window').click(function () {
        var btn = $(this),
            ticket = btn.find('.ticket').html(),            
            recipe =  window.open('','PrintWindow','width=600,height=600'),
            html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + ticket + '</div></body></html>';
        recipe.document.open();
        recipe.document.write(html);
        recipe.document.close();
        return false;
    });
});
</script>

但是更好的解决方案是给一个按钮一个唯一的ID,并onclick打开一个现有的(php生成的)页面从服务器传递该ID,例如/getBooking.php?id=123,该页面将输出任何需要的