How do I prevent form input data from being reset when I remove and re-append the form element?
I have a form that needs to be appended elsewhere in the DOM, and when that occurs all form inputs return to their original values. Is 开发者_JAVA技巧there a way to retain the form input values when the form element is removed and re-appended? I'm using jQuery and the append() function.
This is what worked for me:
Before the form element is cloned using .clone(true):
$('#MyForm :input').each(function() { $(this).data('val',$(this).val()); });
After the cloned form element is append()'d:
$('#MySameFormSomewhereElseInTheDOM :input').each(function() { $(this).val($(this).data('val')); });
精彩评论