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:
// /
(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 );