简体   繁体   English

限制 jQuery select2 最大选中选项

[英]Limit the jQuery select2 maximum selected options

I have referred to this docs provided by select2 jQuery plugin.我已经参考了 select2 jQuery 插件提供的这个文档。 http://ivaynberg.github.io/select2/ http://ivaynberg.github.io/select2/

But when I use this code to limit the number of options a user can select at a time:但是当我使用此代码来限制用户一次可以选择的选项数量时:

$(document).ready(function(){
    $(".select2").select2({ maximumSelectionSize: 2 });
    });

And here is the html of select tag:这是选择标签的html:

  <select id="store-name" name="state[]" class="select2-select-00 span6" multiple size="5"
            data-placeholder="Store" style="height:25px;">


        <?php foreach ($this->Store->get_all_stores_names() as $row) {

        //print_r($row);
          echo '<option value="' . $row->name . '"> ' . $row->name . '</option>';
        }
        ?>

    </select>

When I try to limit it, I get this error in console:当我尝试限制它时,我在控制台中收到此错误:

Uncaught TypeError: Object [object Object] has no method 'select2'未捕获的类型错误:对象 [object Object] 没有方法“select2”

Why?为什么? I mean the select2 works fine which means that it's js files are being loaded, then why I am unable to limit it?我的意思是 select2 工作正常,这意味着它正在加载 js 文件,那为什么我无法限制它?

Use maximumSelectionLength like so:像这样使用maximumSelectionLength

$("#selectQQQ").select2({
    maximumSelectionLength: 3
});

Extension延期

Select2 has data-* attributes Select2 具有data-* attributes

Meaning, that you can set your maximumSelectionLength parameter as HTML5 data attributes like so:意思是,您可以将maximumSelectionLength参数设置为 HTML5 数据属性,如下所示:

<select data-placeholder="Select a state" data-maximum-selection-length="2" ...>

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

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