Javascript 对象[Object Object]没有方法';选择2';

Javascript 对象[Object Object]没有方法';选择2';,javascript,jquery,jquery-select2,script-tag,Javascript,Jquery,Jquery Select2,Script Tag,在这里,我包含了脚本: <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.

在这里,我包含了脚本:

<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:


和页面底部的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>

函数格式值(数据){
返回data.ime_prezime;
}
$('#parcele')。选择2({
阿贾克斯:{
数据类型:“json”,
url:“json.php”,
结果:功能(数据){
返回{结果:数据};
}
},
宽度:“300px”,
formatResult:formatValues,
formatSelection:formatValues,
多重:对
});
但我得到了错误:
uncaughttypeerror:Object[Object Object]没有方法“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
编辑:我发现了您的问题:

  • 在您的实际页面上没有任何带有
    id
    “parcele”的元素-我认为您想到的是带有
    id
    “parcela”的元素(注意“a”而不是“e”)

  • 实际上,您有两个id为“parcela”的
    元素,但HTML id必须是唯一的

  • 要解决这个问题,请使用
    id
    “parcela”重命名其中一个元素,然后在现有JavaScript中有“parcele”的地方使用其中一个元素


    另外,为了验证在修复命名问题时一切正常,我创建了一个正确的文件。

    mu json.php文件很好。。。同样的代码也适用于其他只有此代码的页面…使用像github这样的cdn中的js/css文件,而不是使用github中的js/css文件,这通常会阻止来自它的请求(当被当作cdn处理时)。我尝试了它,但还是一样的,当我再次托管文件时也是一样的。。。我的jquery代码在页面底部,HTML在一个模式窗口中…请看,不错,但对我来说也不起作用:查看浏览器控制台…尝试暂时禁用除jquery之外的所有JavaScript文件,然后选择2.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