我想爲我的項目使用select2插件。我跟着this的例子,但它不適合我。如何加載JSON數據以與select2插件一起使用
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>
這是resul T當我使用console.log
測試我的代碼:
選擇二:Ajax的結果沒有在響應的
results
鍵返回數組。
我在哪裏犯錯?
'djubrivo.php'你得到JSON陣列? – 2015-02-05 22:17:15
yes,offcource我得到這個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」}等等...... – 2015-02-05 22:24:07
從版本3.4.2更改了一些東西,因爲我可以用同樣的方法加載JSON,但是現在當我嘗試4.0.0版本時,這個不工作...而且我的json數組輸出很好...就像docs – 2015-02-05 22:26:23