selectize-未捕获的TypeError:$(...)。selectize不是函数

时间:2019-06-24 01:25:45

标签: javascript jquery html selectize.js

我正在尝试在Web应用程序中使用独立的selectize库。但是出现此错误

Uncaught TypeError: $(...).selectize is not a function

这是我的代码: 在标题中

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript" src="../js/selectize.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="../css/selectize.default.css" >

在html文件中

<label for="input-tags3">Skills</label>
<input id="input-tags3" name="skills" type="text" th:field="*{skills}" class="demo-default selectized text-input" value="science,biology,chemistry" tabindex="-1" style="display: none;">
<div class="selectize-control demo-default multi plugin-remove_button">
    <div class="selectize-input items not-full has-options has-items">
    <div class="item" data-value="science">science<a href="javascript:void(0)" class="remove" tabindex="-1" title="Remove">×</a></div>
    <div class="item" data-value="biology">biology<a href="javascript:void(0)" class="remove" tabindex="-1" title="Remove">×</a></div>
    <div class="item" data-value="chemistry">chemistry<a href="javascript:void(0)" class="remove" tabindex="-1" title="Remove">×</a></div>
    <input type="text" autocomplete="off" tabindex="" id="input-tags3-selectized" style="width: 4px; opacity: 1; position: relative; left: 0px;">
</div>
    <div class="selectize-dropdown multi demo-default plugin-remove_button" style="display: none; width: 520px; top: 36px; left: 0px; visibility: visible;">
    <div class="selectize-dropdown-content">
    <div class="option" data-selectable="" data-value="physics">physics</div>
    </div>
   </div>
 </div>

在js文件中

$('#input-tags3').selectize({
    plugins: ['remove_button'],
    delimiter: ',',
    persist: false,
    create: function(input) {
        return {
            value: input,
            text: input
        }
    }
});

缺少什么?

2 个答案:

答案 0 :(得分:1)

您应按如下所示用$('select').selectize();包裹$(function() {})以便初始化jQuery原型。

<script type="text/javascript" src="selectize.js"></script>
<link rel="stylesheet" type="text/css" href="selectize.css" />
<script>
$(function() {
    $('select').selectize(options);
});
</script>

请参阅here以获取详细信息。


PS :对于options,您可以参考以下示例:

...
$select_city = $('#select-city').selectize({
  valueField: 'name',
  labelField: 'name',
  searchField: ['name']
});
select_city = $select_city[0].selectize;
...
select_city.disable();
...

答案 1 :(得分:0)

在document.ready函数的开头添加jquery后已解决的问题。

$(document).ready(function(){
$('#input-tags3').selectize({
    plugins: ['remove_button'],
    delimiter: ',',
    persist: false,
    create: function(input) {
        return {
            value: input,
            text: input
        }
    }
});
....
});