gpt4 book ai didi

twitter-bootstrap - Bootstrap 3 typeahead.js - 通过 typeahead val 的一部分进行查询

转载 作者:行者123 更新时间:2023-12-04 04:41:36 25 4
gpt4 key购买 nike

我正在尝试使用输入值的最后一部分来调用我的远程 url。
我想做这样的事情:

    $('#typeahead').typeahead({
remote: {
url: '/ajax/tags/get/?name=%QUERY',
replace: function (url, query) {
var last = query.split(',');
last = $.trim(last[last.length-1]);
return url.replace('%QUERY', last);
}
},
limit : 10
});

and when dropdown item selected,

enter image description here

将新值添加到行尾

enter image description here

知道如何使它工作吗?

最佳答案

对于 Bootstrap 3,您可以使用 Bootstrap-3-Typeahead .

您将不得不覆盖 updatermatcher功能。对于matcher函数,您可以使用替换函数中的代码,如下所示:

matcher: function (item) {
var last = this.query.split(',');
this.query = $.trim(last[last.length-1]);

if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}

为您的 update 使用以下代码:
updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}

(匹配最后一次出现: JavaScript: replace last occurrence of text in a string)

完整示例:
$('.typeahead').typeahead (
{
items: 4,
source: function (query, process) {
states = [];
map = {};


var data = [
{"stateCode": "CA", "stateName": "California"},
{"stateCode": "AZ", "stateName": "Arizona"},
{"stateCode": "NY", "stateName": "New York"},
{"stateCode": "NV", "stateName": "Nevada"},
{"stateCode": "OH", "stateName": "Ohio"}
];

$.each(data, function (i, state) {
map[state.stateName] = state;
states.push(state.stateName);
});

process(states);

}

, updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
, matcher: function (item) {
var last = this.query.split(',');
this.query = $.trim(last[last.length-1]);

if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}
}

);

关于twitter-bootstrap - Bootstrap 3 typeahead.js - 通过 typeahead val 的一部分进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323650/

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