我将 Select2JS 4.0.5 与 PHP 和 jQuery 3.2.1 一起使用。首先包含 jQuery,然后是 select2js,然后是其余的 JS 库。我的代码
if ($("#company").length > 0){ // The code below works with typeaheadJS
$('#company').select2()({
source: function (query, syncResults, asyncResults) {
$.ajax({
url: "common/search.php",
data: 'query=' + query,
dataType: "json",
type: "POST",
success: function (data) {
asyncResults($.map(data, function (item) {
return item;
}));
}
});
}
});
}
我不断得到
Error: No select2/compat/inputData
类似的问题说如果我包含完整版本而不是这个错误将被修复。如果我这样做,错误会变成
TypeError: $(...).select2(...) is not a function
从
input
标签更改为 select
标签对我有用。
你需要包括完整版,
select2.full.js
此版本支持输入类型,而不仅仅是选择。
代替:
import 'select2'
我做了:
import 'select2/dist/js/select2.full'
验证您是否没有其他人放置 Id
#company
我遇到了这个问题,我在我的页面中找到了另一个使用相同 Id 的组件
不是您确切问题的解决方案,但它可能会帮助解决此错误的人。
使用 Razor Pages.
在渲染之前,将your own
id
属性添加到@Html.DropDownListFor()
。好像是helper用的默认的不行啊
在您看来:
@Html.DropDownListFor(Model => Model.SelectedFile, Model.GetFilesSelectList(), "Select item", new { @id= "SelectedFileId" })
请注意,在这种情况下,通常生成的
id
将是“SelectedFile”。我们正在用new { @id= "SelectedFileId" } dictionary
覆盖它。
然后在您的 JS 中进行通常的选择:
$(document).ready(function () {
// Variable assignment
var $producerSelect = $('#SelectedProducerId');
var $fileSelect = $('#SelectedFileId');
// Initialize your dropdownlists...
)};
删除前两个括号:
$('#company').select2()({