简体   繁体   English

用jQuery复制select2选择的问题

[英]Issue with duplicating select2 selects with jQuery

I'm working on a project where we need to create an expandable form, so the user can input any number of items by clicking an 'add new item' button which will clone a row and allow them to select new options to add into the form. 我正在做一个需要创建可扩展表格的项目,因此用户可以通过单击“添加新项目”按钮输入任意数量的项目,该按钮将克隆一行并允许他们选择要添加到新项目中的选项。形成。

To do this so far we have a row of elements as shown: 到目前为止,要完成此操作,我们有一排元素,如下所示:

<div class="input-group">
  <select class="form-control name-select" name="name">
    <option value disabled selected>Component Name</option>
  </select>
  <select class="form-control type-select" name="sale_type">
    <option value disabled selected>Sale Type</option>
    <option value="1">type 1</option>
    <option value="2">type 2</option>
  </select>
  <input type="input" class="form-control" placeholder="number">
  <input type="text" class="form-control" placeholder="Total number" readonly>
  <input type="text" class="form-control" placeholder="Cost per item" readonly>
  <input type="text" class="form-control" placeholder="Total cost" readonly>
</div>

We are currently just working on the front end, but these selects will have lots of options in. 我们目前仅在前端工作,但是这些选择将包含很多选项。

My issue is that we need to be able to duplicate this row and still be able to use the select2 boxes. 我的问题是,我们需要能够复制这一行,并且仍然能够使用select2框。 From looking into it we realise that we need to destroy the select2, clone it, then reinitialise it. 通过查看它,我们意识到我们需要销毁select2,将其克隆,然后重新初始化。 This is not an issue, but for some reason, when we duplicate the row of items, all select2 boxes except the most recently created one are just normal selects. 这不是问题,但是由于某些原因,当我们复制项目行时,除了最近创建的一个选择框之外的所有select2框都只是普通选择。 It's as if it's only allowing one of each of the select2's which isn't ideal because we need all of them to work to allow ease of selection from the long list of options. 好像它只允许每个select2都不理想,因为我们需要所有它们都起作用才能使从一长串选项中轻松选择。

The code we are using to duplicate the items is: 我们用来复制项目的代码是:

var base = element.find('.item-row.base').first();

base.find('.name-select').select2('destroy');
base.find('.type-select').select2('destroy');

var itemRowClone = base.clone();

itemRowClone.removeClass('base')

itemRowClone.find('select').each(function(){
    $(this).attr('name', $(this).attr('name') + element.find('.item-row').length)
});

element.find('.new-items').append(itemRowClone);

base.find('.name-select').select2();
base.find('.type-select').select2();

itemRowClone.find('.name-select').select2();
itemRowClone.find('.type-select').select2();

console.log('reinitialised')

Like I said, it's destroying the select2s just fine, but when it re-initialises them it's only allowing one instance of each select to be select2, the rest are all default selects. 就像我说的,销毁select2s很好,但是当它重新初始化它们时,只允许每个select的一个实例为select2,其余的都是默认选择。

If anyone has any ideas then that would be amazing! 如果有人有任何想法,那就太好了!

Thanks in advance 提前致谢

In the end, I got it working with by duplicating the select2 tags as it was, without destroying it. 最后,我通过复制select2标签而不破坏它来使其工作。 Then manually removing the span tag which is generated by the select2 which is directly after the select tag. 然后,手动删除由select2直接在select标签之后生成的span标签。 Then I removed the class select2-hidden-accessible , and the attribute data-select2-id and then called the select2() method on the select tags. 然后,我删除了类select2-hidden-accessible和属性data-select2-id ,然后在选择标记上调用了select2()方法。

itemRowClone.find('span.select2').remove();
itemRowClone.find('select').removeClass('select2-hidden-accessible');
itemRowClone.find('select').removeAttr('data-select2-id');

arrangement.find('.new-items').append(itemRowClone);

itemRowClone.find('.name-select').select2();
itemRowClone.find('.type-select').select2();

Hope this helps someone else in this situation! 希望在这种情况下能对其他人有所帮助!

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

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