将值设置为使用AJAX填充的选择

时间:2015-03-05 15:11:13

标签: jquery ajax jquery-select2 angularjs-select2

我使用Select2进行两次选择(城市和ZIP)。我希望当我选择邮政编码时,它会动态设置城市(也相反)。

我尝试了多种解决方案,但没有任何效果:

这是我的代码:

$('.input-address-city-name').select2({
    ajax: {
        url: ROOT_URL,
        dataType: 'json',
        delay: 250,
        data: function(params) {
            return {
                q: params.term, // search term
            };
        },
        processResults: function(data, page) {
            var array = new Array();
            $(data).each(function(index, city) {
                var obj = new Object();
                obj = city;
                obj.id = city.id;
                obj.text = city.name;
                array.push(city);
            });
            return {
                results: array
            };
        },
    },
    minimumInputLength: 2,
    placeholder: {
        id: "-1",
        text: "Localité"
    },
    templateSelection: function (res) {
        return res.text;
    },
});

$('.input-address-city-zip').select2({
    ajax: {
        url: ROOT_URL+'/app/address-by-zip',
        dataType: 'json',
        delay: 250,
        data: function(params) {
            return {
                q: params.term, // search term
            };
        },
        processResults: function(data, page) {
            var array = new Array();
            $(data).each(function(index, city) {
                var obj = new Object();
                obj = city;
                obj.id = city.id;
                obj.text = city.ZIP;
                array.push(city);
            });
            return {
                results: array
            };
        },
    },
    minimumInputLength: 2,
    placeholder: {
        id: "-1",
        text: "NPA"
    },
    templateSelection: function (res) {
        return res.text;
    }
});

我试过这些案子:

$('.input-address-city-name').on("change", function(e){
    $('.input-address-city-zip').val({id: "12", text: "00000"}).trigger("change");
})

$('.input-address-city-name').on("change", function(e){
    $('.input-address-city-zip').val("test").trigger("change");
})

templateSelection: function (res) {
        $('.input-address-city-zip').val({id: "12", text: "00000"}).trigger("change");
        return res.text;
    }

没有什么工作,是因为我使用了远程数据吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果这对你有用,你可以尝试这个。它对我有用。

$('.input-address-city-name').on("change", function(e){
var id = "12";
$('.input-address-city-zip').val([id]);
$('.input-address-city-zip').select2({
    ajax: {
    url: ROOT_URL,
    dataType: 'json',
    delay: 250,
    data: function(params) {
        return {
            q: params.term, // search term
        };
    },
    processResults: function(data, page) {
        var array = new Array();
        $(data).each(function(index, city) {
            var obj = new Object();
            obj = city;
            obj.id = city.id;
            obj.text = city.name;
            array.push(city);
        });
        return {
            results: array
        };
    },
},
minimumInputLength: 2,
placeholder: {
    id: "-1",
    text: "Localité"
},
templateSelection: function (res) {
    return res.text;
},
});
})

尝试再次使用ajax函数绑定它。您还应该看到如何提供要选择的值。

  $('#id').val(["123","124"]);