开发者

How do I prevent form input data from being reset when I remove and re-append the form element?

开发者 https://www.devze.com 2023-02-02 07:27 出处:网络
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 t

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')); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号