简体   繁体   中英

Click a link on page unload event

I have multiple forms in one page, each of them with their unique save action.

I want to implement an "auto-save" feature and my idea, when user navigates away without saving its form, it's to catch the page unload event and click on the existing save buttons.

Here's the piece of JS code:

window.addEventListener('unload', function(){
  $('#saveLink').click();
  console.log('Save link clicked.');
}, false);

I got all console logs written, but none of the links are clicked.

What may be wrong?

Any help would be appreciated.

Thanks.

您应该调用由click事件触发的函数,而不仅仅是触发click事件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM