0

我在这件事上遇到了大约一天的麻烦。我正在使用带有 Select2 的 jQuery 中继器。当我单击添加按钮时,它初始化了新的 select2 但破坏了现有的。我找到了解决方案。但是现在,虽然每个 select 都初始化为 select2,但新的 select 上没有任何选项。

<div class="col-md-6 col-12">
    <div class="form-group">
        <label for="user_id">Quem</label>
        <select multiple="multiple" name="user_id" id="user_id" class="form-control select2 quem-tag"
                aria-describedby="user_id" {{$readonly ? 'disabled' : ''}}>
                @foreach($users as $user)
                    @if (in_array('non-compliance', $user->userPermissions->pluck('slug')->toArray()))
                        <option value="{{$user->id}}" 
                            @foreach($nonCompliance->actionsUsers as $actionUser)
                                {{($actionUser->user_id ?? old("user_id", "")) == $user->id ? "SELECTED" : ""}}
                            @endforeach>
                            {{$user->name}} ({{$user->sector->name}})
                        </option>
                    @endif
                @endforeach
                @if(isset($action->externalPeople) && sizeof($action->externalPeople) > 0)
                    @foreach($action->externalPeople as $extPerson)
                        <option value="{{$extPerson->name}}"
                            {{($extPerson->name ?? old("user_id", "")) == $extPerson->name ? "SELECTED" : ""}}>
                            {{$extPerson->name}}
                        </option>
                    @endforeach>
                @endif
        </select>
    </div>
</div>

下面是我的中继器。以上是select2的第一次初始化。出于某种原因,如果此代码高于第一个,它不会重新初始化。

  $("#actions-list").repeater({
    show: function () {
      $(this).slideDown(function(){ 
        $(this).find('select2').select2({
          // the following code is used to disable x-scrollbar when click in select input and
          // take 100% width in responsive also
          dropdownAutoWidth: true,
          width: "100%",
          dropdownParent: $(this).parent(),
        });

        if($(this).find('select2').hasClass('quem-tag')){
          $(this).find('select2').select2({
            tags: true,
            placeholder: 'Selecione'
          })
        }
      });      
    },
    hide: function (deleteElement) {
      if (confirm("Tem certeza que quer excluir a ação?"))
        $(this).slideUp(deleteElement);
    },
  });

我真的不知道可能是什么问题。它应该如下所示:

第一个选择

但它只是一个漂亮的输入文本

重复选择

4

0 回答 0