Home  >  Article  >  Web Front-end  >  Whether monitoring data changes based on jquery_jquery

Whether monitoring data changes based on jquery_jquery

WBOY
WBOYOriginal
2016-05-16 18:08:01846browse

In this way, the previous efforts were in vain. The solution to these problems is to monitor whether the page data changes. If changes occur, prompt the user to save. If the data has not changed. When we click save, there is no need to submit to the database.
Let’s look at the solution:

Copy the code The code is as follows:

// /
(function($) {
var pageDataChange = false //The default logo page data has not changed
/* Monitor whether the page data changes*/
$.fn.MonitorDataChange = function(options) {
var tagName = new Array('Input', 'Select', 'Textarea');
var ctrlIds = [];

var deafult = {
arrTags: tagName, //The tagName attribute array of the control to be monitored
arrCtrls: ctrlIds //The ID of the control not to be monitored
};
var ops = $.extend(deafult, options);

for (var i = 0; i < ops.arrTags.length; i ) {
$(ops.arrTags[i]). each(function() {
if (ops.arrCtrls.length == 0) {
$(this).bind('change', function() {
pageDataChange = true;
} );
}
else {
var flag = false;
for (var j = 0; j < ops.arrCtrls.length; j ) {
if ($(this) .attr('id') == ops.arrCtrls[j]) {
flag = true;
break;
}
}
if (!flag) {
$ (this).bind('change', function() {
pageDataChange = true;
});
}
}
});
}
return this ;
};
/*Return whether the page data has changed*/
$.fn.getValue = function() {
return pageDataChange;
};
})(jQuery );
Statement:
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