How JavaScript checks the form data for changes

Source: Internet
Author: User
You need to check to see if the user has modified the contents of a form, you can use the method provided in this article, return True if the contents of the form are modified, return false without modification, and a friend with a need can refer to the next

Sometimes, you need to check to see if the user has modified the contents of a form, you can use the following tips, which returns true if the contents of the form are modified, and False if the contents of the form are not modified. The code is as follows:

Copy Code code as follows:


function Formisdirty (form) {
for (var i = 0; i < form.elements.length; i++) {
var element = Form.elem Ents[i];
var type = Element.type;
if (type = = CheckBox | | | type = = "Radio") {
if (element.checked!= element.defaultchecked) {
return true;
}
}
Else if (type = = Hidden | | | type = = "Password" | | | type = = "Text" | | | type = = "textarea") {
if (ele Ment.value!= element.defaultvalue) {
return true;
}
}
Else if (type = = Select-one | | | type = = "Select-multiple") {
for (var j = 0; J < Element.optio Ns.length; J + +) {
if (element.options[j].selected!= element.options[j].defaultselected) {
return true;
}
}
}
}
return false;
}
Window.onbeforeunload = function (e) {
E = e | | window.event;
if (Formisdirty (document.forms["Someform"])) {
if (e) {
E.returnvalue = "You have unsaved changes.";
}
Return "have unsaved changes."
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.