简体   繁体   English

JQuery,通过按钮创建表单输入不适用于表单列表

[英]JQuery, Creating form inputs via button not working with form list

so I have this basic bootstrap form and I have a button called add another location which will dynamically create another 4 inputs to add more location.所以我有这个基本的引导程序表单,我有一个名为添加另一个位置的按钮,它将动态创建另外 4 个输入以添加更多位置。 This is achieved via jquery and jquery UI.这是通过 jquery 和 jquery UI 实现的。 So I made 3 copies of this form and put them in a list because eventually, they are going to come from a server and loop the form depends on however many sets of information available.所以我制作了这个表格的 3 份副本并将它们放在一个列表中,因为最终它们将来自服务器并循环表格取决于可用的信息集。 The problem I am having is that, only my first add another location button works and it's creating additional inputs on the second and third one as well.我遇到的问题是,只有我的第一个添加另一个位置按钮有效,并且它也在第二个和第三个创建额外的输入。 I can give different id/class to the buttons where the new form goes but that wouldn't do me any good since more or fewer forms can be displayed via the server.我可以为新表单所在的按钮提供不同的 id/class,但这对我没有任何好处,因为可以通过服务器显示更多或更少的表单。 My question is how can each button act independently without giving different id/class to it.我的问题是每个按钮如何独立运行而不给它不同的 id/class。 so if I click add another location button on the second set of form, it only creates additional inputs on the second set not first or 3rd, same for 1st set and 3rd set.因此,如果我单击第二组表单上的添加另一个位置按钮,它只会在第二组而不是第一组或第三组上创建其他输入,第一组和第三组相同。

this is the jquery code that clones and appends the new inputs这是克隆和附加新输入的 jquery 代码

    $("#pm-do-clone1").click(function () {

    $(".pm-clone-this3 .pm-clone4").clone().appendTo(".pm-clone-here1");
});

here's my jsfiddle : https://jsfiddle.net/jaisilchacko/yqvd4Lvv/4/ ps: the fiddle the first add location is not creating new inputs, but it works on my local.Probably an external resource issue这是我的 jsfiddlehttps : //jsfiddle.net/jaisilchacko/yqvd4Lvv/4/ ps:第一个添加位置的小提琴不是创建新输入,但它适用于我的本地。可能是外部资源问题

alright, as I understand You gotta grab the clicked button by referancing it with;好吧,据我所知,您必须通过引用来抓住点击的按钮;

 $("#pm-do-clone1").click(function () {
 $(this).//rest of the code

and at the rest of the code as we captured the clicked one, we now have to find its parent where we gonna add the new inputs.在我们捕获点击的代码的其余部分,我们现在必须找到它的父级,我们将在其中添加新的输入。 For example,例如,

var y = document.createElement('input');
var x =$(this).parents('.form');
$(x).append(y);

Edit: Btw, you are clicking an ID, ID is not the best model to catch one from multiple elemets because it sometimes make mistakes, use class instead.编辑:顺便说一句,您正在单击一个 ID,ID 不是从多个元素中捕获一个的最佳模型,因为它有时会出错,请改用类。

Edit2: Check this snippet too. Edit2:也检查这个片段。 I belive this will help you.我相信这会对你有所帮助。 View DEMO 查看演示

Edit3: Why do you wrapping each inputs into divs? Edit3:你为什么要把每个输入都包装成 div? It seems not necessary no create too much elements as you could achive the same result with only inputs.似乎没有必要创建太多元素,因为您只需输入即可获得相同的结果。

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

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