javascript – 如何加载JSON数据以将其与select2插件一起使用

前端之家收集整理的这篇文章主要介绍了javascript – 如何加载JSON数据以将其与select2插件一起使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的项目使用select2插件.我遵循了 this的例子,但对我来说并不奏效.

JSON输出

[
    {"ime":"BioPlex TM"},{"ime":"Aegis sym agrilla"},{"ime":"Aegis sym irriga"},{"ime":"Aegis sym microgranulo"},{"ime":"Aegis sym pastiglia"},{"ime":"Agroblen 15816+3MgO"},{"ime":"Agroblen 18816+3MgO"},{"ime":"Agrobor 15 HU"},{"ime":"Agrocal (Ca + Mg)"},{"ime":"Agrocal (Ca)"},{"ime":"Agrogold"},{"ime":"Agroleaf Power 12525+ME"},{"ime":"Agroleaf Power 151031+ME"},{"ime":"Agroleaf Power 202020+ME"},{"ime":"Agroleaf Power 311111+ME"},{"ime":"Agroleaf Power Ca"},{"ime":"Agrolution 14714+14 CaO+ME"},{"ime":"Agrovapno dolomitno"},{"ime":"Agrovit HSF"},{"ime":"Agrovit P"},{"ime":"Agrozin 32 T"},{"ime":"Albatros Hydro"},{"ime":"Albatros Sprint"},{"ime":"Albatros Standard"},{"ime":"Albatros Universal"},{"ime":"Algaren"},{"ime":"AlgoVital ? Plus"},{"ime":"Amalgerol PREMIUM"},{"ime":"Amcolon \/ Novalon"},{"ime":"Amcopaste"},{"ime":"Aminosprint N8"},{"ime":"AminoVital"},{"ime":"Ammonium nitrate 33.5%"},{"ime":"Ammonium nitrate with calcium sulfate"},{"ime":"Ammonium sulfate"}
]

脚本:

function formatDjubrivo(data) {
    return data;
}
function formatDjubrivo1(data) {
    return data.ime;

$( "#inputs" ).change(function() {
    console.log('prolazi klik');
    var t = $( this ).val();
    console.log(t);
    if (t=='djubrivo') {
       console.log('prolazi klik if');
       $('#stavka').select2({
          ajax: {
             dataType : "json",url      : "djubrivo.PHP",results  : function (data) {
                 return {results: data};
             }
          },formatResult : formatDjubrivo
       });
    }else {
       console.log('nije djubrivo');
    }
});

HTML:

<div class="col-md-2" style="padding-right:0px;">
    Vrsta Inputa
    <select id="inputs" name="inputs" class="form-control js-example-responsive">
        <option value="djubrivo">djubrivo</option>
        <option value="pesticidi">pesticidi</option>
        <option value="kultura">kultura</option>
        <option value="voda">voda</option>
    </select>
</div>

<div class="col-md-2" style="padding-right:0px;">
    Stavka
    <input id="stavka" name="stavka" class="form-control js-example-responsive">
</div>

这是当我使用console.log测试我的代码的结果:

Select2: The AJAX results did not return an array in the results key of the response.

我在哪里犯错?

解决方法

看起来您正在使用Select2 4.0,从您提供的链接到示例和您收到的错误消息.然而,您的代码是针对以前版本的Select2编写的.

如果要继续使用Select2 4.0:

(1)将结果ajax选项更改为processResults.

(2)更改processResults函数,使其返回的对象的result属性是一个对象数组,其中每个对象都有一个id和一个text属性.一种方法是使用$.map()函数从ajax调用返回函数创建一个新的数组.

processResults: function (data) {
    return {
        results: $.map(data,function(obj) {
            return { id: obj.ime,text: obj.ime };
        })
    };
}

您也可以摆脱formatResult选项.

(3)使用< select>元素,而不是< input>元件.

<select id="stavka" name="stavka" class="form-control js-example-responsive"></select>

jsfiddle

原文链接:https://www.f2er.com/js/152718.html

猜你在找的JavaScript相关文章