在select2 multiselect中加载值 [英] Load values in select2 multiselect

查看:158
本文介绍了在select2 multiselect中加载值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用select2代替搜索框.

I'm using select2 in place of search box.

在这里,我正在使用这样的国家/地区值

Here i'm using to load countries values like this

$("#countries").select2({
    multiple: true,
    tags:["India", "Japan", "Australia","Singapore"],
    tokenSeparators: [","]
});

当我按保存按钮时,它们已正确提交到服务器,现在这里的问题是 当我要保存到服务器后要修改国家/地区字段时,如何将保存的值加载到国家/地区字段.

When i press save button, they are properly submitted to the server, now here the question is when i want to modify the country field after saving to server, how i can load the saved values to the country field.

这是我从服务器检索数据的方式

This is how i retrieve data from server

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {

    //the opts here contains the json values of the countries.

    //what code should be written here to load the values in $('#countries).select2();

    //user can add some more countries or can remove the previously added countries. 

}

推荐答案

我只是使用此 http://ivaynberg.github.io/select2/#event_ext_change 链接,并使用触发函数加载值

I just use this http://ivaynberg.github.io/select2/#event_ext_change link and use the trigger function to load the values

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {
        parent.parent.parent.showLoader();
        if (opts) {
            $("#countries").val(opts).trigger("change");
        } 

此技巧会将值加载到选择框中.

This trick load the value in the select box.

这篇关于在select2 multiselect中加载值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆