[英]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. 我有一个需要在DOM中其他位置附加的表单,当这种情况发生时,所有表单输入都将返回其原始值。 Is 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.
我正在使用jQuery和append()函数。
This is what worked for me: 这对我有用:
Before the form element is cloned using .clone(true): 在使用.clone(true)克隆form元素之前:
$('#MyForm :input').each(function() { $(this).data('val',$(this).val()); });
After the cloned form element is append()'d: 将克隆的表单元素添加到append()之后:
$('#MySameFormSomewhereElseInTheDOM :input').each(function() { $(this).val($(this).data('val')); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.