问题
I have a form and button in it called Print and Submit
The click on that button calls Ajax request which is creating PDF and open it in Print Window using PrintJS plugin ( http://printjs.crabbly.com/ )
I want to call $('#form').submit()
after print requests ( Print or Cancel )
I've found this solution https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/ but it doesn't work in my case. It works only when I'm using CTRL + P combination
回答1:
Looks like Promise is what you are looking for.
(Printjs .print() returns promise)
Something like:
import { Print } from 'printjs'
Print.print(...).then($('#form').submit())
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
回答2:
This is a tricky problem. I checked the link you shared and yes it works with ctrl+p only and that too once (in my case) I would suggest to try and add confirm box and take input from user. You can add confirm box afterprint method and check on the input from user. Based on user input you can call the submit() method. But still it is tricky one.
来源:https://stackoverflow.com/questions/47987759/js-how-to-detect-the-print-requests-of-the-print-window