防止打印对话框刷新页面


Prevent print dialog from refreshing page

我有一张发票,我已经为它设置了一个打印按钮:

</script>
<!--Function for printing invoice-->
<script>
function printpage()
  {
  window.print()
  }
</script>
<button id="print" name="print" class="btn btn-info"onClick="printpage()">Print Invoice</button>

现在单击按钮,打印对话框按预期出现,但是,如果我取消打印选项,发票页面刷新,所有信息丢失。是否有一种方法可以取消打印对话框,但不刷新页面?

我怀疑我需要引用对话框的单击事件,但这是我遇到困难的地方。谢谢的

为了方便将来使用,按钮HTML被修改为:

<button id="print" name="print" type="button"class="btn btn-info"onClick="printpage()">Print Invoice</button>

这将在取消打印对话框时阻止刷新页面,从而防止丢失从DB动态生成/拉入的任何信息。

我试着重现你的问题,但我唯一能做的就是把按钮放在一个表单中。

输入type="button"以阻止它成为提交表单的提交按钮

尝试从onclick处理程序返回false:

onClick="printpage(); return false"