简体   繁体   中英

“[object Object]” showing in input field with bootstrap3-typeahead and tagsinput

When a tag is selected, the tag is added correctly but the text [object Object] is appended in the input box. (See the below image)

I am using Bootstrap 3, JQuery, Bootstrap3-typeahead, and bootstrap-tagsinput (All the most recent versions)

The form is in a modal window.

标签错误

Here is the code:

jQuery('#banner_geo_locations').tagsinput({
    itemValue: 'value',
    itemText: 'text',
    typeahead: {
        displayKey: 'text',
        source: function (query) {
            return jQuery.get("http://dev.marijuanaweeklycoupons.com/?action=ajax|cityStateSsearchTags&PageSpeed=off&q=" + query);
        }
    }
});

Ajax looks like:

[{"value":"2908762","text":"Dixon Corner, AL"},
{"value":"2956030","text":"Dixon Corner, ME"},
{"value":"3008251","text":"Dixon Corner, PA"},
{"value":"2931983","text":"Dixon Crossroads, GA"},
{"value":"3017149","text":"Dixon Crossroads, SC"},
{"value":"2959449","text":"Dixon Estates, MD"},
{"value":"2959450","text":"Dixon Hill, MD"},
{"value":"2993971","text":"Dixon Landing, NC"},
{"value":"2908763","text":"Dixon Shop, AL"},
{"value":"2938722","text":"Dixon Springs, IL"}]

See it in a fiddle

It is a library issue.

Adding the below solves the issue:

afterSelect: function(val) { this.$element.val(""); },

As such,

jQuery('#banner_geo_locations').tagsinput({
    itemValue: 'value',
    itemText: 'text',
    typeahead: {
        displayKey: 'text',
        afterSelect: function(val) { this.$element.val(""); },
        source: function (query) {
            return jQuery.get(current_web_root + "?action=ajax|cityStateSearchTags&PageSpeed=off&q=" + query);
        }
    }
});

See jsfiddle

https://jsfiddle.net/rgaxuyda/11/

Lucky Edward, it works, thanks.

<script>
    var elt = $('#inputSoftwareList');
    elt.tagsinput({
        itemValue: 'value',
        itemText: 'text',
        typeahead: {
            source: function(query) {
                return $.getJSON('cities.json');
            },
            afterSelect: function(val) { this.$element.val(""); }
        }
    });

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM