【问题标题】:How to change a select2 4.0 selected item that gets data from ajax如何更改从 ajax 获取数据的 select2 4.0 选定项目
【发布时间】:2016-02-26 20:02:53
【问题描述】:

我正在尝试制作一个编辑表单。此表单必须加载来自数据库的数据并显示来自数据库的数据。在我的表单中,我有一个通过 ajax 加载项目的 select2 组合框。我需要使用来自数据库的 id 值更改此组合框的选定索引。我用来加载数据的代码是:

function populateState(){


var urlCities='@routes.StateCtrl.lstStatesByCountryToSelect()';
$("#idCountry").select2({
    placeholder: "Select country...",
    ajax: {
    url: urlCities,
    dataType: 'json',
    type: 'GET',
        data: function(term,page){
                return{q:term,idCountry:$("#idCountry").val()};
                },
        results: function(data, page) {
            return {
                results: data.results
            };
        }
    }
});

}

我正在尝试这个,但它不起作用:

$("#idCountry").select2().select2("val", id);

例如,如果我在页面加载时在组合框中加载三个州(德克萨斯州、纽约州、佛罗里达州),我想在组合框中显示德克萨斯州作为初始选定项。

【问题讨论】:

标签: javascript jquery ajax playframework select2


【解决方案1】:

$("#idCountry").val("desired_value").trigger("change");

更多信息可以在 Deprecated and removed methods 下找到: https://select2.org/upgrading/migrating-from-35

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多