Jquery 使用多个关键字自动完成
我不确定这是否可能,但我想让jQueryUIAutoComplete使用多个关键字来获得相同的结果 这是一个很旧的jquery文件,但我似乎无法让它工作,即使使用较旧的jquery文件。我不太熟悉jquery和javascript,但我可以编辑现有的东西 这是我目前拥有的(没有对多关键字进行任何调整):Jquery 使用多个关键字自动完成,jquery,jquery-ui,autocomplete,Jquery,Jquery Ui,Autocomplete,我不确定这是否可能,但我想让jQueryUIAutoComplete使用多个关键字来获得相同的结果 这是一个很旧的jquery文件,但我似乎无法让它工作,即使使用较旧的jquery文件。我不太熟悉jquery和javascript,但我可以编辑现有的东西 这是我目前拥有的(没有对多关键字进行任何调整): $(文档).ready(函数(){ NewAuto(); }); 函数NewAuto(){ var乘积=[ 若我正确理解了你们的问题,你们想显示和同一句子的多个单词相匹配的列表 例如,单击
$(文档).ready(函数(){
NewAuto();
});
函数NewAuto(){
var乘积=[
若我正确理解了你们的问题,你们想显示和同一句子的多个单词相匹配的列表
例如,单击
测试
.srchHilite{背景:黄色;}
$(文档).ready(函数(){
NewAuto();
});
函数NewAuto(){
var availableTags=[“赢得一天”、“赢得某人的心”、“赢得某人的心”];
警惕(可用标记);//警惕=赢得一天,赢得某人的心,赢得某人的心
$(“#标记”).autocomplete({
来源:函数(requestObj、responseFunc){
var matchArry=availableTags.slice();//复制数组
var srchTerms=$.trim(requestObj.term).split(/\s+/);
//对于每个搜索词,删除不匹配项。
$.each(srchTerms,function(J,term){
var regX=新的RegExp(术语“i”);
matchArry=$.map(matchArry,函数(项){
返回注册表测试(项)?项:空;
});
});
//返回匹配结果。
responseFunc(matchArry);
},
打开:功能(事件、用户界面){
//这个函数不提供结果列表的挂钩,所以我们现在必须信任选择器。
var resultsList=$(“ul.ui-autocomplete>li.ui-menu-item>a”);
var srchTerm=$.trim($(“#标记”).val()).split(/\s+/).join(“|”);
//循环浏览结果列表并突出显示术语。
resultsList.each(函数(){
var jThis=$(本);
var regX=new RegExp(“(“+srchTerm+”)”,“ig”);
var oldTxt=jThis.text();
html(oldTxt.replace(regX,$1');
});
}
});
}
多词搜索:
我从数据库中获取城市区域名称,并使用自动完成功能进行显示
function GetLocalityList() {
var LocalityArray = [];
$.post("MvcLayer/Index/GetLocalityList",
{
CityID: $('#sltCity').val()
},
function(data) {
// My sql query will be like this select LocalityID, CityID, LocalityName from tablename where CityID = 20
// Here (data) is array format. Like this
// [{"LocalityID":9397,"CityID":55,"LocalityName":"Adugodi"},{"LocalityID":9398,"CityID":55,"LocalityName":"Aga Abbas Ali Road"},{"LocalityID":9399,"CityID":55,"LocalityName":"Agaram"},{"LocalityID":9400,"CityID":55,"LocalityName":"Agrahara Dasara Halli"},{"LocalityID":9401,"CityID":55,"LocalityName":"Agrahara Dasarahalli"},{"LocalityID":9402,"CityID":55,"LocalityName":"Airport Exit Road"},{"LocalityID":9403,"CityID":55,"LocalityName":"Horamavu"},{"LocalityID":9404,"CityID":55,"LocalityName":"Hosakere Halli"},{"LocalityID":9405,"CityID":55,"LocalityName":"Hennur"},{"LocalityID":9406,"CityID":55,"LocalityName":"Hesaraghatta"},{"LocalityID":9407,"CityID":55,"LocalityName":"HKP Road"},{"LocalityID":9408,"CityID":55,"LocalityName":"HMT Layout"},{"LocalityID":9409,"CityID":55,"LocalityName":"Hongasandra"},{"LocalityID":9410,"CityID":55,"LocalityName":"Hoody"},{"LocalityID":9411,"CityID":55,"LocalityName":"Hayes Road"} ]
$.each(data, function(key, value) {
LocalityArray[key] = value.LocalityName;
});
$("#txtLocality" + SelectedTab).autocomplete({
minLength: 1,
source: function(req, responseFn) {
// \\b show each match letter in each word of list
// ^ show each match letter in whole word of list
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(req.term), "i");
var a = $.grep(LocalityArray, function(item, index) {
return matcher.test(item);
});
responseFn(a);
}
});
},
'json'
);
}
“多关键字”是什么意思?你能给出一些例子吗?你链接的例子很好用,不是吗?我在网上找到了这个例子,但在我的代码和多关键字中无法使用,如果你看这个例子,你会发现当你键入“赢”和“天”时,你会得到“赢一天”您的JSFIDLE工作正常。我没有收到任何错误。发布您的完整代码和收到的错误。看起来这也会在您键入第二个单词时关闭,请在我的代码和示例中尝试,但no go=(您能准确解释您想要什么吗?如果您要键入“win”和“day”在一起通常你不会得到结果,但我仍然想得到结果“赢得这一天”K我现在得到了。我会尽快回答。你的例子()对我来说很好。我没有任何错误。我已经更新了我的答案(你的小提琴例子)。检查它。在发布完整代码后,你仍然没有得到它。因此我们可以帮助你。
<html>
<head>
<title>Testing</title>
<link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.srchHilite { background: yellow; }
</style>
<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="scripts/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
NewAuto();
});
function NewAuto() {
var availableTags = ["win the day", "win the heart of", "win the heart of someone"];
alert(availableTags); // alert = win the day,win the heart of,win the heart of someone
$("#tags").autocomplete({
source: function(requestObj, responseFunc) {
var matchArry = availableTags.slice(); // Copy the array
var srchTerms = $.trim(requestObj.term).split(/\s+/);
// For each search term, remove non-matches.
$.each(srchTerms, function(J, term) {
var regX = new RegExp(term, "i");
matchArry = $.map(matchArry, function(item) {
return regX.test(item) ? item : null;
});
});
// Return the match results.
responseFunc(matchArry);
},
open: function(event, ui) {
// This function provides no hooks to the results list, so we have to trust the selector, for now.
var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
var srchTerm = $.trim($("#tags").val()).split(/\s+/).join('|');
// Loop through the results list and highlight the terms.
resultsList.each(function() {
var jThis = $(this);
var regX = new RegExp('(' + srchTerm + ')', "ig");
var oldTxt = jThis.text();
jThis.html(oldTxt.replace(regX, '<span class="srchHilite">$1</span>'));
});
}
});
}
</script>
</head>
<body>
<div>
<label for="tags">
Multi-word search:
</label>
<input type="text" id="tags" />
</div>
</body>
</html>
function GetLocalityList() {
var LocalityArray = [];
$.post("MvcLayer/Index/GetLocalityList",
{
CityID: $('#sltCity').val()
},
function(data) {
// My sql query will be like this select LocalityID, CityID, LocalityName from tablename where CityID = 20
// Here (data) is array format. Like this
// [{"LocalityID":9397,"CityID":55,"LocalityName":"Adugodi"},{"LocalityID":9398,"CityID":55,"LocalityName":"Aga Abbas Ali Road"},{"LocalityID":9399,"CityID":55,"LocalityName":"Agaram"},{"LocalityID":9400,"CityID":55,"LocalityName":"Agrahara Dasara Halli"},{"LocalityID":9401,"CityID":55,"LocalityName":"Agrahara Dasarahalli"},{"LocalityID":9402,"CityID":55,"LocalityName":"Airport Exit Road"},{"LocalityID":9403,"CityID":55,"LocalityName":"Horamavu"},{"LocalityID":9404,"CityID":55,"LocalityName":"Hosakere Halli"},{"LocalityID":9405,"CityID":55,"LocalityName":"Hennur"},{"LocalityID":9406,"CityID":55,"LocalityName":"Hesaraghatta"},{"LocalityID":9407,"CityID":55,"LocalityName":"HKP Road"},{"LocalityID":9408,"CityID":55,"LocalityName":"HMT Layout"},{"LocalityID":9409,"CityID":55,"LocalityName":"Hongasandra"},{"LocalityID":9410,"CityID":55,"LocalityName":"Hoody"},{"LocalityID":9411,"CityID":55,"LocalityName":"Hayes Road"} ]
$.each(data, function(key, value) {
LocalityArray[key] = value.LocalityName;
});
$("#txtLocality" + SelectedTab).autocomplete({
minLength: 1,
source: function(req, responseFn) {
// \\b show each match letter in each word of list
// ^ show each match letter in whole word of list
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(req.term), "i");
var a = $.grep(LocalityArray, function(item, index) {
return matcher.test(item);
});
responseFn(a);
}
});
},
'json'
);
}