简体   繁体   English

删除并重新添加表单元素时,如何防止表单输入数据被重置?

[英]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.

相关问题 调用 remove() 后如何重新追加元素? - How do I re-append elements after calling remove()? 如何防止表单重置清除我的 <input type=“text”> 元件? - How can I prevent a form reset from clearing my <input type=“text”> element? 如何在表单数据后附加元素? (Jquery的) - How do I append an element with form data? (Jquery) 如何防止将热键 ` 添加到 html 表单输入中? - How do I prevent the hotkey ` from being added to a html form input? 如何防止将无效字符输入表单? - How do I prevent invalid characters from being entered into a form? 空后如何将可拖动元素重新添加到可放置元素 - How to re-append draggable element to droppable after empty 如何“清除”附加的第一个活动 class 并重新附加当前活动的 class? - How can I "clear" the first active class appended and re-append the current active class? Javascript重新添加一个Removed元素 - Javascript Re-append a Removed element 如何在保留现有表单数据的同时使用 append 输入节点与 JavaScript 形成? - How do I append input node to form with JavaScript while keeping existing form data? 删除文本(使用 JavaScript 删除)后无法在元素中重新附加文本(使用 JavaScript 附加) - Unable to re-append a text in an element (using JavaScript append) after removing text (using JavaScript remove)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM