Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在select2 jquery插件中未调用initSelection_Javascript_Jquery_Jquery Select2 - Fatal编程技术网

Javascript 在select2 jquery插件中未调用initSelection

Javascript 在select2 jquery插件中未调用initSelection,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我正在使用来自的Select2jQuery插件 我正在使用以下代码 $(document).ready(function () { $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, ajax: { // instead of writing the function to

我正在使用来自的Select2jQuery插件

我正在使用以下代码

 $(document).ready(function () {
            $("#e6").select2({
                placeholder: "Search for a movie",
                minimumInputLength: 1,
                ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
                    dataType: 'jsonp',
                    quietMillis: 1000,
                    data: function (term, page) {
                        return {
                            q: term, // search term
                            page_limit: 10,
                            apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
                        };
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter remote JSON data
                        return { results: data.movies };
                    }
                },
                initSelection: function (element, callback) {
                    var id = 9942;//$(element).val();
                    alert('initSelection');
                    if (id !== "") {
                        $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
                            data: {
                                apikey: "ju6z9mjyajq2djue3gbvv26t"
                            },
                            dataType: "jsonp"
                        }).done(function (data) { callback(data); });
                    }
                },

                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
                escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
            });

        });


        $(document).ready(function () {
            $("#e6").on("select2-selecting", function (e) {
                var v = 10;
                alert("selecting val=" + e.val + " choice=" + JSON.stringify(e.choice));
                var id = document.getElementById('<%= savebtn.ClientID %>');
                id.value = e.val;
                id.click();

            });
        });
$(文档).ready(函数(){
$(“#e6”)。选择2({
占位符:“搜索电影”,
最小输入长度:1,
ajax:{//我们使用Select2的方便助手,而不是编写函数来执行请求
url:“http://api.rottentomatoes.com/api/public/v1.0/movies.json",
数据类型:“jsonp”,
安静百万:1000,
数据:功能(术语,第页){
返回{
q:term,//搜索术语
页数限制:10页,
apikey:“ju6z9mjyajq2djue3gbvv26t”//请不要使用,这样这个示例就可以继续工作
};
},
结果:函数(数据,页面){//将结果解析为Select2所需的格式。
//因为我们使用的是自定义格式函数,所以不需要更改远程JSON数据
返回{results:data.movies};
}
},
initSelection:函数(元素,回调){
var id=9942;//$(元素).val();
警报('initSelection');
如果(id!==“”){
$.ajax(”http://api.rottentomatoes.com/api/public/v1.0/movies/“+id+”.json”{
数据:{
apikey:“ju6z9mjyajq2djue3gbvv26t”
},
数据类型:“jsonp”
}).done(函数(数据){回调(数据);});
}
},
formatResult:movieFormatResult,//为简洁起见省略,请参阅此页的源代码
formatSelection:movieFormatSelection,//为简洁起见省略,请参阅此页的源代码
dropdownCssClass:“bigdrop”//应用使下拉列表更高的css
escapeMarkup:function(m){return m;}//我们不想转义标记,因为我们在结果中显示html
});
});
$(文档).ready(函数(){
$(“#e6”)。在(“选择2选择”,功能(e){
var v=10;
警报(“选择val=“+e.val+”choice=“+JSON.stringify(e.choice));
var id=document.getElementById(“”);
id.value=e.val;
id.单击();
});
});

问题:由于某些原因,initSelection没有被调用,因此,我无法设置回帖中文本框的值

我正在使用从站点加载远程数据的示例

我查看了for initSelection,它说“只有在有初始输入要处理时才会调用此函数”。我不确定它的确切含义


我做错什么了吗?请帮助

您应该提供初始值:

<input ... value="192" ... />
对于多个值,请执行以下任一操作:

<input ... value="192,193" ... />

请参阅。

对于更复杂的情况,我就是这么做的,因为如果不控制initalvalue,initalvalue会保存到db(在coffeescript中)


如果您不想在默认情况下使用某个值,您可以这样做

$(document).ready(function () {
            $("#e6").select2({
        ...
                },
                initSelection: function (element, callback) {
        ...
                },
                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
                escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
            }).select2('val', []);

        });
添加
select2('val',[])在声明结束时解决此问题。

使用以下方法:

删除以前的选项使用以下选项选择的选项:

       arr = json.filter(function( obj ) {
            return  arrSelectedValues.indexOf(obj.foo) == -1;
        });
并创建一个选定的新元素:

        option = document.createElement('option');
        option.value = 'foo';
        option.innerHTML = 'foo';
        option.selected = true;

        document.getElementById('selectFoobares').appendChild(option);

这不是最好的方法,但有效

这是正确的。即使您提供了initSelection函数,select2也决定不调用它,除非您提供了“value”attr。因此,您必须使用一个值attr,或者提供一个值而不使用它。问题是,这对多个选定选项不起作用。
value
属性必须为非空-
value=”“
也不起作用。您可以将任意字符串放在那里,但必须有某些内容。这比在HTML中使用伪值要好得多,谢谢!
$(document).ready(function () {
            $("#e6").select2({
        ...
                },
                initSelection: function (element, callback) {
        ...
                },
                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
                escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
            }).select2('val', []);

        });
       arr = json.filter(function( obj ) {
            return  arrSelectedValues.indexOf(obj.foo) == -1;
        });
        option = document.createElement('option');
        option.value = 'foo';
        option.innerHTML = 'foo';
        option.selected = true;

        document.getElementById('selectFoobares').appendChild(option);