You can add form="myformid"
attribute to elements that are outside form to include it in form. I would add data-dirty="false"
attribute to all elements at the beginning and attach change event that would change data-dirty attribute of changing element to true. Then you can save form each 30 seconds for example (get elements that have data-change=true and pass to server). After saving, every element's data-dirty becomes false again. Example of autosave with jQuery:
window.setInterval(function(){
var dirtyElements = $('#myformid').find('[data-dirty=true]').add('[form=myformid][data-dirty=true]');
if(dirtyElements.length > 0){
var data = dirtyElements.serialize();
$.post('saveurl', data, function(){
dirtyElements.attr('data-dirty', false);
alert('data saved successfully');
});
}
}, 30000); // 30 seconds
Attaching event to all elements of form:
$(function(){
var formElements = $('#myformid')
.find('input, select, textarea')
.add('[form=myformid]')
.not(':disabled')
.each(function(){
$(this).attr('data-dirty', false).change(function(){
$(this).attr('data-dirty', true);
});
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…