Home > Web Front-end > JS Tutorial > JavaScript method to check whether form data has changed_javascript skills

JavaScript method to check whether form data has changed_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 17:27:47
Original
1226 people have browsed it

Sometimes, you need to check whether the user has modified the contents of a form. You can use the following technique. If the contents of the form have been modified, it will return true, and if the contents of the form have not been modified, it will return false. The code is as follows:

Copy code The code is as follows:

function formIsDirty(form) {
for (var i = 0; i < form.elements.length; i ) {
var element = form.elements[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 (element.value != element.defaultValue) {
return true ;
}
}
else if (type == "select-one" || type == "select-multiple") {
for (var j = 0; j < element. options.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 "You have unsaved changes.";
}
};
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template