用户输入后如何打印页面?

时间:2014-10-06 17:23:22

标签: javascript printing

我正在尝试使用以下代码打印部分页面:

<script type="text/javascript">
    $("#btnPrint").live("click", function () {
        var divContents = $("#PrintContent").html();
        var printWindow = window.open('', '', 'height=400,width=800');
        printWindow.document.write('<html><head><title>My Print Title</title>');
        printWindow.document.write('</head><body>');
        printWindow.document.write(divContents);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
    });
</script>

一切都很好但是当用户更改了某些输入(打印div内的表单)时,例如单击复选框后,打印窗口仍显示原始页面(空表单或初始值)。如何让用户在输入时打印页面?

感谢。

1 个答案:

答案 0 :(得分:0)

表单的状态保存在DOM中,而不是HTML中。在调用jQuery的html()函数时,你会失去你的价值观。相反,您必须克隆整个jQuery对象并将其附加到新窗口。

$("#btnPrint").on("click", function () {
  var divContents = $("#PrintContent").clone();
  var printWindow = window.open('', '', 'height=400,width=800');

  divContents.appendTo(printWindow.document.body);
  printWindow.print();
});

http://jsfiddle.net/NameFILIP/up6p0zk5/

相关问题