老版本select2设置初始值

废话不多说;我说的老版本就是要求初始元素为input;

<input id='example' value='2'/>

下面是data格式

data: [
                    {
                        "id": "1",
                        "text": "张三"
                    },
                    {
                        "id": "2",
                        "text": "李师师"
                    },
                    {
                        "id": "3",
                        "text": "林冲"
                    },
                    {
                        "id": "4",
                        "text": "周通"
                    },{
                        "id": "5",
                        "text": "杨康"
                    }
                ],

 

$('#example').select2({

  placeholder:'默认值',//上面给了value;这里就不起作用了

  ajax:{

    //省略

    results:function () {}

  },

  initSelection: function (element, callback) {

    var initId = $(this).val();

    var initData = {id: initId, text:'默认值'}

    callback(initData)

  }

})

应该就是这样了。大概就是这样,来不及测了;

posted @ 2017-06-14 19:45  流年之外天空蓝  阅读(2515)  评论(0编辑  收藏  举报