【问题标题】:different selectize dropdown should have same dropdown option values while search搜索时不同的选择下拉菜单应具有相同的下拉选项值
【发布时间】:2021-01-16 21:22:16
【问题描述】:

有两个(或更多)选择下拉列表,其中下拉值(选项值)由用户创建。问题是当用户在两个选择下拉列表中的任何一个中创建一些值时,两个选择应该具有相同的下拉值。下拉列表值应该相同,并加载到其他下拉列表值中,而不管它们在哪里创建(添加)。

参考链接:-https://selectize.github.io/selectize.js/

小提琴链接:- http://jsfiddle.net/1oy2d6e9/

html

<section class="demo" id="demo-single-item-select">
                <div class="header">
                    Single Item Select
                </div>
                <div class="sandbox">
                    <label for="select-beast">Beast:</label>
                    <select id="select-beast" class="demo-default" placeholder="create a person by typing...">
                    </select>
                    
                </div>
                         <div class="sandbox">
                    <label for="select-beast">Beast2:</label>
                    <select id="select-beast2" class="demo-default" placeholder="create a person by typing...">
                    </select>
                    
                </div>
   <div class="description">
                    These two select box have same values when values are created in either of the two.
                    <a href="https://selectize.github.io/selectize.js/">https://selectize.github.io/selectize.js/</a>
                </div>
</section>

js

var gArr = [];

// selectize should load all dropdown values ie in gArr initially;
     $('#select-beast2').selectize({
                        create: true,
                        sortField: 'text',
                        searchField: 'item',
                        create: function(input) {
                          gArr.push(input);  //<=======storing in global variable
                            return {
                                value: input,
                                text: input
                        }
    }
                    });
                    
                     $('#select-beast').selectize({
                        create: true,
                        sortField: 'text',
                        searchField: 'item',
                        create: function(input) {
                          gArr.push(input);  //<=======storing in global variable
                            return {
                                value: input,
                                text: input
                        }
    }
                    });

【问题讨论】:

    标签: javascript jquery selectize.js


    【解决方案1】:

    create 函数下,您可以获得需要添加选项的另一个选择框,然后使用addOption 向该选择框添加新选项。

    演示代码

    $('#select-beast2').selectize({
      create: true,
      sortField: 'text',
      searchField: 'item',
      create: function(input) {
        var selectize = $('#select-beast')[0].selectize; //get another slect box
        selectize.addOption({
          value: input,
          text: input
        }); //add option to it
       
        return {
          value: input,
          text: input
        }
      }
    });
    
    $('#select-beast').selectize({
      create: true,
      sortField: 'text',
      searchField: 'item',
      create: function(input) {
        var selectize = $('#select-beast2')[0].selectize; //get another slec box
        selectize.addOption({
          value: input,
          text: input
        }); //add option to it
       
        return {
          value: input,
          text: input
        }
      }
    });
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.11.0/css/selectize.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.11.0/js/standalone/selectize.js"></script>
    <section class="demo" id="demo-single-item-select">
      <div class="header">
        Single Item Select
      </div>
      <div class="sandbox">
        <label for="select-beast">Beast:</label>
        <select id="select-beast" class="demo-default" placeholder="create a person by typing...">
        </select>
    
      </div>
      <div class="sandbox">
        <label for="select-beast">Beast2:</label>
        <select id="select-beast2" class="demo-default" placeholder="create a person by typing...">
        </select>
    
      </div>
      <div class="description">
        These two select box have same values when values are created in either of the two.
    
      </div>
    </section>

    【讨论】:

    • 谢谢,选择输入会自动填充其他选择输入。不应填充其他选择用户搜索输入,只应更新下拉值。
    • 删除 selectize.addItem(input); 应该可以工作的行。更新的代码也看看。
    • Swati,如果有 100 个多重选择,则 var selectize = $('#select-beast2')[0].selectize;这应该如何改变。有没有通用的解决方案。
    • 是的,您可以使用每个循环遍历所有选择并使用$(this) 而不是id。这是sample 代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多