dongpeixiong5943 2016-10-28 09:20
浏览 224

Bootstrap 3 Typeahead不显示结果

I'm using Bootstrap 3 Typeahead with ajax call. The function returns correctly the datas but they don't appear.

Here my code:

$('#txtComune').typeahead({
        minLength: 2,
        delay: 400,
        items: 5,
        source: function (query, response) {
            $.ajax({
                url: '<?= $root; ?>/get_comuni',
                dataType: "json",
                type: 'POST',
                data: {
                    query: query
                },
                success: function (data) {
                    response(data.comuni);
                }
            });
        },
        displayText: function (item) {
            console.log(item.comune);
            return item.comune;
        }
    });

The console.log function returns all city's names but the dropdown list doesn't appear in the textbox.

Here my textbox code:

<input name="txtComune" class="form-control input-md" data-provide="typeahead" autocomplete="off" id="txtComune" type="text" placeholder="" maxlength="250">

Here my JSON results:

{
"exception": false,
"comuni": [{
    "idComune": 1332,
    "comune": "Caraffa di Catanzaro",
    "pv": "CZ",
    "cap": "88050",
    "attivo": 1
}, {
    "idComune": 1831,
    "comune": "Catania",
    "pv": "CT",
    "cap": "951xx",
    "attivo": 1
}, {
    "idComune": 1832,
    "comune": "Catanzaro",
    "pv": "CZ",
    "cap": "88100",
    "attivo": 1
}, {
    "idComune": 3240,
    "comune": "Gravina di Catania",
    "pv": "CT",
    "cap": "95030",
    "attivo": 1
}, {
    "idComune": 4076,
    "comune": "Militello in Val di Catania",
    "pv": "CT",
    "cap": "95043",
    "attivo": 1
}, {
    "idComune": 6258,
    "comune": "San Gregorio di Catania",
    "pv": "CT",
    "cap": "95027",
    "attivo": 1
}]
}
  • 写回答

1条回答 默认 最新

  • doushi3715 2016-10-28 09:22
    关注

    Please change this:

    source: function (query, response) {
            $.ajax({
                url: '<?= $root; ?>/get_comuni',
                dataType: "json",
                type: 'POST',
                data: {
                    query: query
                },
                success: function (data) {
                    response(data.comuni);
                }
            });
    }
    

    TO

    source: function (query, response) {
        return $.get('<?= $root; ?>/get_comuni', { query: query }, function (data) {
            console.log(data);
            return response(data);
        });
    }
    

    Here is an example: jsfiddle

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的