我已经参考了select2 jQuery插件提供的此文档。http://ivaynberg.github.io/select2/
但是当我使用此代码限制用户一次可以选择的选项数量时:
$(document).ready(function(){
$(".select2").select2({ maximumSelectionSize: 2 });
});
这是select标签的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>
当我尝试限制它时,我在控制台中收到此错误:
未捕获的TypeError:对象[object Object]没有方法'select2'
为什么?我的意思是select2正常工作,这意味着它正在加载js文件,那么为什么我无法对其进行限制?
使用maximumSelectionLength就像这样:
$("#selectQQQ").select2({
maximumSelectionLength: 3
});
扩展名
Select2具有data-* attributes
意思是,您可以像这样将data-* attributes
参数设置为HTML5数据属性:
maximumSelectionLength