简体   繁体   English

没有 select2/compat/inputData

[英]No select2/compat/inputData

I'm using Select2JS 4.0.5 with PHP and jQuery 3.2.1.我将 Select2JS 4.0.5 与 PHP 和 jQuery 3.2.1 一起使用。 jQuery is included first, then right after that select2js then after that the rest of JS libraries.首先包含 jQuery,然后是 select2js,然后是 rest 的 JS 库。 My code我的代码

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;
                }));
            }
        });
    }
});
} 

I keep getting Error: No select2/compat/inputData我一直收到Error: No select2/compat/inputData

Similar questions said that if I include the full version instead this error will be fixed.类似的问题说如果我包含完整版本而不是这个错误将被修复。 I if I do that, the error would become如果我这样做,错误将变成

TypeError: $(...).select2(...) is not a function

对我来说,将输入标签更改为选择标签有效。

你需要包含完整版, select2.full.js这个版本支持输入类型,而不仅仅是选择。

Instead of:而不是:

import 'select2'

I did:我做了:

import 'select2/dist/js/select2.full'

Verify if you don't have others places the Id #company验证您是否没有其他人放置 Id #company

I had this problem and i find another component using the same Id in my page我遇到了这个问题,我发现另一个组件在我的页面中使用相同的 Id

Not the solution to your exact problem, but it may help someone with this error.不是您确切问题的解决方案,但它可以帮助解决此错误的人。

Using Razor Pages.使用 Razor 页。

Before rendering, add your own id attribute to the @Html.DropDownListFor() .在呈现之前,将您自己的id属性添加到@Html.DropDownListFor() It seems to be that the default one used by the helper does not work.好像是helper用的默认的不行。

In your view:在您看来:

@Html.DropDownListFor(Model => Model.SelectedFile, Model.GetFilesSelectList(), "Select item", new { @id= "SelectedFileId" })

Notice that in this case, normally the id generated would be "SelectedFile".请注意,在这种情况下,通常生成的id将是“SelectedFile”。 We are overwriting that with new { @id= "SelectedFileId" } dictionary .我们正在用new { @id= "SelectedFileId" } dictionary覆盖它。

And in your JS make the usual selection:在你的 JS 中做出通常的选择:

$(document).ready(function () {

    // Variable assignment
    var $producerSelect = $('#SelectedProducerId');
    var $fileSelect = $('#SelectedFileId');

    // Initialize your dropdownlists...
)};

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

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