gpt4 book ai didi

javascript - Bootstrap 2.2.1 提前输入

转载 作者:行者123 更新时间:2023-11-30 08:56:29 25 4
gpt4 key购买 nike

PHP,返回一个JSON编码的数组

$this->load->model('car_model', 'cars');
$result = $this->cars->searchBrand($this->input->post('query'));
$this->output->set_status_header(200);
$this->output->set_header('Content-type: application/json');
$output = array();
foreach($result as $r)
$output['options'][$r->brandID] = $r->brandName;
print json_encode($output);

输出:{"options":{"9":"Audi","10":"Austin","11":"Austin Healey"}}

JS 更新:

$(".searchcarBrands").typeahead({
source: function(query, typeahead) {
$.ajax({
url: site_url + '/cars/search_brand/'+query,
success: function(data) {
typeahead.process(data);
},
dataType: "json"
});
},
onselect: function(item) {
$("#someID").val(item.id);
}
});

更新:未捕获的类型错误:对象函数 (){return a.apply(c,e.concat(k.call(arguments)))} 没有方法 'process'

如果我只输入“A”,则预先输入只会显示每个结果的第一个字母(一堆 A 字母)。如果我输入第二个字母,我就什么也看不到了。

我已经尝试对数据使用 JSON.parse 或使用 data.options 但没有成功。

我做错了什么?

最佳答案

最后一天,我一直在使用 Bootstrap 2.2.1 解决这个问题。无论我做什么,都行不通。对我来说,除非我在流程函数中放置一个断点,否则我总是会遇到流程未定义的错误(可能只是因为 FireBug 已打开?)。

无论如何,作为一个补丁,我重新下载了省略了 typeahead 的 Bootstrap,从这里获得了 typeahead: https://gist.github.com/2712048

并使用了这段代码:

$(document).ready(function() {
$('input[name=artist]').typeahead({
'source': function (typeahead) {
return $.get('/7d/search-artist.php', { 'artist': typeahead.query }, function (data) {
return typeahead.process(data);
});
},
'items': 3,
'minLength': 3
},'json')
});

我的服务器返回这个(对于'Bo'):

["Bo","Bo Burnham","Bo Diddley","Bo Bruce","Bo Carter",
"Eddie Bo","Bo Bice","Bo Kaspers Orkester","Bo Saris","Bo Ningen"]

当然,现在它忽略了我的 minLength,但它会让我度过这一天。希望这会有所帮助。

编辑:在这里找到解决方案: Bootstrap 2.2 Typeahead Issue

使用 Bootstrap 2.2.1 中包含的 typeahead,代码应为:

    $(document).ready(function() {        $('input[name=artist]').typeahead({            'source': function (query,typeahead) {                return $.get('/search-artist.php', { 'artist': encodeURIComponent(query) }, function (data) {                    return typeahead(data);                });            },            'items': 3,            'minLength': 3        },'json')    });

关于javascript - Bootstrap 2.2.1 提前输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13252421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com