【问题标题】:Select2: add option from the search fieldSelect2:从搜索字段中添加选项
【发布时间】:2017-02-17 13:44:39
【问题描述】:

我想知道是否有人可以帮助我,我有一个带有一些预定义选项的选择标签,现在我想要的是当我在 select2 搜索字段中搜索一个不存在的选项时,使用插入的搜索值创建一个选项.

例如。当我输入选项中不存在的四时,实际创建一个值为四的选项。我希望我能很好地解释我的问题。

<select name='selectized' id='selectized' class='form-control'>
    <option value='1'>one</option>
    <option value='2'>two</option>
    <option value='3'>three</option>
</select>

<script>
    $(document).ready(function() {
        $('#selectized').select2({
            placeholder: 'sometext',
            allowClear: false
        });
    });
</script>

【问题讨论】:

    标签: javascript jquery select select2


    【解决方案1】:

    将tags: true 添加为this 示例显示

    $('#selectized').select2({
          placeholder: 'sometext',
          allowClear: false,
          tags: true
    });
    

    $('#selectized').select2({
      placeholder: 'sometext',
      allowClear: false,
      tags: true
    });
    #selectized {
      width: 200px
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />
    
    <select name='selectized' id='selectized' class='form-control'>
       <option value='1'>one</option>
       <option value='2'>two</option>
       <option value='3'>three</option>
    </select>

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2015-11-02
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多