1

我将 Select2 用于两个选择(City 和 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;
    }

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

谢谢你的帮助

4

1 回答 1

0

YOu can try this if this works for you . It worked for me .

$('.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;
},
});
})

try binding it again with the ajax function.You should also see how to provise values to select.

  $('#id').val(["123","124"]);
于 2015-03-05T15:36:08.783 回答