简体   繁体   中英

Way to check input element changes in a form

Is there anyway to check any of the HTML components (ie input elements) value in a form got changed so that i can pop up a alert saying "would like to save changes?" before navigating to other page.

Thanks.

Since you have jQuery in your question:

After page load, use $.serialize to create a snapshot of the form.

On submit, run serialize again.

Then you can use use (or create) an isEqual function to compare if there has been a change.

This question might help you on that last step: How to determine equality for two JavaScript objects?

EDIT

jQuery also has a serializeArray method which may make that process easier.

https://api.jquery.com/serializeArray/

jQuery('.form_component').change(function (){..}); , where form_component should be a class common for all your input elements in the form.

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