简体   繁体   English

动态表单字段添加会清除所有字段的输入

[英]Dynamic form field adding clears the input for all fields

I have created a form that you can dynamically add and remove fields to. 我创建了一个表单,可以动态向其中添加和删除字段。 I am so close to getting it working, but for some reason I can fill in the first fields, add a new field and the first field data remains. 我已经很接近要开始工作了,但是由于某种原因,我可以填写第一个字段,添加一个新字段,然后保留第一个字段数据。 Then when I add a second field, fill it in and add another, it will clear all existing data. 然后,当我添加第二个字段时,填写并添加另一个字段,它将清除所有现有数据。

I can tell that the flatpickr calendar is always on the first field, but then when I add subsequent fields, the flatpickr calendar only applies to the last field and won't actually show for any in between. 我可以说出flatpickr日历始终在第一个字段上,但是当我添加后续字段时, flatpickr日历仅适用于最后一个字段,而实际上不会在这两个字段之间显示。 I really need some help as I've spent so long trying to figure it out :( 我花了很长时间试图找出答案,我真的需要一些帮助:(

I believe this is an issue with my JS and how it gets the last element, not how it calls the flatpickr library. 我相信这是我的JS的问题,以及它如何获取最后一个元素,而不是它如何调用flatpickr库。

Here is all my code in a JSFiddle: https://jsfiddle.net/danboy4/w9s9ayep/ 这是我在JSFiddle中的所有代码: https ://jsfiddle.net/danboy4/w9s9ayep/

Thanks! 谢谢!

You were right with your assumption that the error was located at how it gets the last element. 您的假设是正确的,即错误位于如何获取最后一个元素。 You had the following code: 您有以下代码:

var html = $(".copy-fields").html();
$(".after-add-more").after(html);

var lastInput = $(".control-group").closest(".start").prevObject 
var lastInput2 = lastInput[lastInput.length-2].getElementsByClassName("start")[0];

This would add the new copied fields directly after the first input box but when getting the element from the array you wanted to use flatpickr on you'd use lastInput.length-2 . 这将在第一个输入框之后直接添加新复制的字段,但是当从数组中获取元素时,您要使用lastInput.length-2就可以使用lastInput.length-2 This index would always be the index of the LAST visible box. 该索引将始终是LAST可见框的索引。 The problem was that the box you newly added was not the LAST visible box but actually the second one (because it is directly added after the first input box). 问题在于您新添加的框不是最后一个可见框,而是实际上的第二个框(因为它是在第一个输入框之后直接添加的)。 Therefore it would overwrite the previous values. 因此它将覆盖先前的值。

So if you just change var lastInput2 = lastInput[lastInput.length-2].getElementsByClassName("start")[0]; 因此,如果仅更改var lastInput2 = lastInput[lastInput.length-2].getElementsByClassName("start")[0]; to var lastInput2 = lastInput[1].getElementsByClassName("start")[0]; 改为var lastInput2 = lastInput[1].getElementsByClassName("start")[0]; it'll work as it's supposed to be. 它会按预期工作。

Alternatively you could leave the var lastInput2 = lastInput[lastInput.length-2].getElementsByClassName("start")[0]; 或者,您可以保留var lastInput2 = lastInput[lastInput.length-2].getElementsByClassName("start")[0]; as it is and instead change where the new element is placed. 照原样,而是更改放置新元素的位置。 To always place it at end you could do something like this $("#button").before(html); 要始终将其放置在末尾,您可以执行以下操作: $("#button").before(html);

Hope I could help you. 希望我能为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM