这里我包含了脚本:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js'></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link href="http://ivaynberg.github.com/select2/select2-3.3.2/select2.css" rel="stylesheet" type="text/css" />
<script src="http://ivaynberg.github.com/select2/select2-3.3.2/select2.js"></script>
HTML:
<input type="hidden" id="parcele" />
和页面底部的jquery代码:
<script>
function formatValues(data) {
return data.ime_prezime;
}
$('#parcele').select2({
ajax: {
dataType: "json",
url: "json.php",
results: function (data) {
return {results: data};
}
},
width: "300px",
formatResult: formatValues,
formatSelection: formatValues,
multiple: true
});
</script>
但出现错误:
Uncaught TypeError: Object [object Object] has no method 'select2'
怎么了我不明白...
最佳答案
您需要等待jQuery和select2.js
加载完毕:
function formatValues(data) {
return data.ime_prezime;
}
$(document).ready(function() { // add this
$('#parcele').select2({
ajax: {
dataType: "json",
url: "json.php",
results: function (data) {
return {results: data};
}
},
width: "300px",
formatResult: formatValues,
formatSelection: formatValues,
multiple: true
});
}); // add this
编辑:我发现了您的问题:
您在http://agroagro.com/template/tema/zadaci.html#的实际页面上没有任何带有
id
“ parcele”的元素-我认为您正在考虑带有id
“ parcela”的元素(请注意,用“ a”代替“ e” )。实际上,您使用
id
“ parcela”包含两个元素,但是HTML ID必须唯一。解决此问题的方法:用
id
“ parcela”重命名其中一个元素,然后在现有JavaScript中具有“ parcele”的位置使用其中之一。另外,只是为了验证是否可以解决命名问题,我创建了this jsFiddle,它可以正常工作。