在select2 initial中设置值

时间:2016-05-27 04:00:01

标签: javascript php jquery html select2

我需要将默认值设置为select2选项select,这是select select和ajax。

这是我到目前为止的代码

$("#firstList").select2({
        placeholder: "Enter Pincode",
    allowClear: true,
     cache: true,
     initSelection : function (element, callback) {
        var data = {id: "IN", text: "INDIA"};
        callback(data);
    },
        ajax: {
            url: "../getZipList",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            delay: 250,
            data: function (params) {
                //console.log(params)
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: 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 the remote JSON data
                return {
                    results: data.items
                };
                //console.log(data.it)
            },
            cache: true
        },
        escapeMarkup: function (markup) {
            return markup;
        }, // let our custom formatter work
        minimumInputLength: 1,
        templateResult: formatRepo, // omitted for brevity, see the source of this page
        templateSelection: formatRepoSelection // omitted for brevity, see the source of this page  


    })

当我尝试这个时

initSelection : function (element, callback) {
    var data = {id: "IN", text: "INDIA"};
    callback(data);
},

初始值未设定......

我也试过

var data = [{
      id: 'value',
      text: 'Text to display'
    }];

    var tags = $("#firstList option").each(function () {
        data.push($(this).val());

    });

但是初始值没有设置。

我正在做什么问题以及如何解决它。帮助请

0 个答案:

没有答案