admin管理员组

文章数量:1311033

I'm trying to populate a select2 element using geonames data. I have a formatSelection method defined but it will not fire when an item is selected.

Here's the HTML element:

<input id="location" size="30" type="text">​

Select2 binding with format functions:

function locationFormatResult(location) {
    var markup = "<table class='location-result'><tr>";

    if (location.countryCode !== undefined) {
        markup += "<td class='flag-image'><img src='/" + location.countryCode.toLowerCase() + ".gif' /></td>";
    }

    markup += "<td class='location-info'>";
    markup += "<div class='location-name'>" + location.name + ", " + location.adminName1 + ", " + location.countryName + "</div>";
    markup += "</td></tr></table>";

    return markup;
}

function locationFormatSelection(location) {
    return location.name + ", " + location.adminName1 + ", " + location.countryName;
}

$(function () {
    $('#location').select2({
        placeholder: 'Location',
        allowClear: true,
        width: '260px',
        minimumInputLength: 2,
        ajax: {
            url: '',
            dataType: 'jsonp',
            data: function (term) {
                return {
                    featureClass: 'P',
                    q: term
                };
            },
            results: function (data) {
                return {
                    results: data.geonames
                };
            }
        },
        formatResult: locationFormatResult,
        formatSelection: locationFormatSelection,
        dropdownCssClass: "bigdrop"
    });
});

You can see the full fiddle here: /

Why is selecting an item not working?

I'm trying to populate a select2 element using geonames data. I have a formatSelection method defined but it will not fire when an item is selected.

Here's the HTML element:

<input id="location" size="30" type="text">​

Select2 binding with format functions:

function locationFormatResult(location) {
    var markup = "<table class='location-result'><tr>";

    if (location.countryCode !== undefined) {
        markup += "<td class='flag-image'><img src='http://www.geonames/flags/x/" + location.countryCode.toLowerCase() + ".gif' /></td>";
    }

    markup += "<td class='location-info'>";
    markup += "<div class='location-name'>" + location.name + ", " + location.adminName1 + ", " + location.countryName + "</div>";
    markup += "</td></tr></table>";

    return markup;
}

function locationFormatSelection(location) {
    return location.name + ", " + location.adminName1 + ", " + location.countryName;
}

$(function () {
    $('#location').select2({
        placeholder: 'Location',
        allowClear: true,
        width: '260px',
        minimumInputLength: 2,
        ajax: {
            url: 'http://ws.geonames/searchJSON',
            dataType: 'jsonp',
            data: function (term) {
                return {
                    featureClass: 'P',
                    q: term
                };
            },
            results: function (data) {
                return {
                    results: data.geonames
                };
            }
        },
        formatResult: locationFormatResult,
        formatSelection: locationFormatSelection,
        dropdownCssClass: "bigdrop"
    });
});

You can see the full fiddle here: http://jsfiddle/6CVbw/1/

Why is selecting an item not working?

Share Improve this question asked Nov 2, 2012 at 16:15 d_ethierd_ethier 3,91125 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

I figured it out. When you instantiate the select2 plugin on an element you have to specify an ID attribute. This worked:

$(function () {
    $('#location').select2({
        id: function(e) { return e.name + '|' + e.adminName1 + '|' + e.countryName },
        placeholder: 'Location',
        allowClear: true,
        width: '260px',
        minimumInputLength: 2,
        ajax: {
            url: 'http://ws.geonames/searchJSON',
            dataType: 'jsonp',
            data: function (term) {
                return {
                    featureClass: 'P',
                    q: term
                };
            },
            results: function (data) {
                return {
                    results: data.geonames
                };
            }
        },
        formatResult: locationFormatResult,
        formatSelection: locationFormatSelection,
        dropdownCssClass: "bigdrop"
    });
});

You can see the updated fiddle here: http://jsfiddle/6CVbw/2/

本文标签: javascriptUnable to select items populated via jsonp in select2Stack Overflow