【问题标题】:Add fixed item and non-removable item to select2?向select2添加固定项目和不可移动项目?
【发布时间】:2021-12-28 07:56:33
【问题描述】:

我有一个邮件模块,我想将提到的 (in the photo) 电子邮件或 hr@example.com 添加到多选 select2 中的 cc 中的 固定选择和不可删除 选项.我怎样才能做到这一点?有什么想法吗?

这是我选择 cc 的 Blade 语法:

<div class="form-group">
    <label>CC <span class="astirered">*</span></label>
    {!! Form::select('cc[]', $employee_list, $mail_cc_user_id, ['class'=>'form-control select2','multiple']) !!}
    <span class="astirered">{!! $errors->first('cc') !!}</span>
</div>

这是选择 2 的脚本:

$('.select2').select2({
    placeholder: "Please select one"
});

【问题讨论】:

    标签: javascript jquery laravel ecmascript-6 jquery-select2


    【解决方案1】:

    我不认为 select 2 有内置的方法,但是通过一些额外的 JQuery 可以隐藏它。如果您使用检查器查看选择框,您会发现类似这样的内容

    <li class="select2-selection__choice" title="mni@email.com" data-select2-id="id">
        <span class="select2-selection__choice__remove" role="presentation">×</span>
        mni@email.com
    </li>
    

    你可以像这样通过 jQuery 删除那个 x:

    $('li.select2-selection__choice[title="mni@email.com"]').find('span.select2-selection__choice__remove').remove();
    

    然后例如,您可以在表单中标记选择的特定电子邮件默认值,然后在文档加载时运行该 jQuery 命令。

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2016-07-18
      • 2021-01-18
      • 2020-04-10
      相关资源
      最近更新 更多