bootstrap typeahead return name and id

前端 未结 6 1636
予麋鹿
予麋鹿 2021-02-03 13:33

Hy! I\'m using twitter bootstraps typeahead:

I\'m calling a page that returns a response with json_encode the page returns a name and an ID,

I want that the type

6条回答
  •  Happy的楠姐
    2021-02-03 14:00

    ......updater: function (item) {
            var item = JSON.parse(item);
            console.log(item.name); 
            $('#VehicleAssignedTechId').val(item.id);       
            return item.name;
        }
    

    On updater of the typeahead call you can put as above code which allows you to add selcted value in the textbox or where you have to put its value in other hidden field.

    Full ajax call is as below:

    $('#VehicleAssignedTechName').typeahead({
        source: function(query, process) {
            var $url =SITE_URL+ 'api/vehicle_techfield_typeahead/' + query + '.json';
            var $items = new Array;
            $items = [""];
            $.ajax({
                url: $url,
                dataType: "json",
                type: "POST",
                success: function(data) {
                    console.log(data);
                    $.map(data, function(data){
                        var group;
                        group = {
                            id: data.id,
                            name: data.name,                            
                            toString: function () {
                                return JSON.stringify(this);
                                //return this.app;
                            },
                            toLowerCase: function () {
                                return this.name.toLowerCase();
                            },
                            indexOf: function (string) {
                                return String.prototype.indexOf.apply(this.name, arguments);
                            },
                            replace: function (string) {
                                var value = '';
                                value +=  this.name;
                                if(typeof(this.level) != 'undefined') {
                                    value += ' ';
                                    value += this.level;
                                    value += '';
                                }
                                return String.prototype.replace.apply('
    ' + value + '
    ', arguments); } }; $items.push(group); }); process($items); } }); }, property: 'name', items: 10, minLength: 2, updater: function (item) { var item = JSON.parse(item); console.log(item.name); $('#VehicleAssignedTechId').val(item.id); return item.name; } });

提交回复
热议问题