简体   繁体   English

检查表单中输入元素更改的方法

[英]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?" 无论如何,是否需要检查表单中的任何HTML组件(即输入元素)值是否已更改,以便我可以弹出警告说“想保存更改”? before navigating to other page. 导航到其他页面之前。

Thanks. 谢谢。

Since you have jQuery in your question: 由于您的问题中包含jQuery

After page load, use $.serialize to create a snapshot of the form. 页面加载后,使用$ .serialize创建表单的快照。

On submit, run serialize again. 提交后,再次运行序列化。

Then you can use use (or create) an isEqual function to compare if there has been a change. 然后,您可以使用(或创建)一个isEqual函数来比较是否发生了更改。

This question might help you on that last step: How to determine equality for two JavaScript objects? 这个问题可能对您的最后一步有所帮助: 如何确定两个JavaScript对象的相等性?

EDIT 编辑

jQuery also has a serializeArray method which may make that process easier. jQuery还有一个serializeArray方法,可以使该过程更容易。

https://api.jquery.com/serializeArray/ 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. ,其中form_component应该是表单中所有输入元素的通用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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