admin管理员组文章数量:1432352
I have a textbox where i want to have an autoplete that lets the user search through addresses. The user must be able to type in different words and the autoplete must search through them to narrow its list.
I've been trying and reading the documentation, but nothing seems to do the trick as it always searches on the whole string instead of the words. Am i missing something?
Example:
When the user enters 'Mathias Antwerp' he must see all the addresses that contain those words. In the example it must show 1 row which is the second one.
<script>
var addresses = [
{ name: "Frederick Dereave Gentstreet 4 Gent" },
{ name: "Mathias Derian Meilaan 9 Antwerp" },
{ name: "Mathias Hors frelaan 5 Kortrijk" }
];
$(document).ready(SetAutoComplete);
function SetAutoComplete() {
$("#testveld").autoplete(emails,
{
matchContains: "word"
}
);
}
</script>
<input type="text" id="testveld" style='width:300px'/>
I have a textbox where i want to have an autoplete that lets the user search through addresses. The user must be able to type in different words and the autoplete must search through them to narrow its list.
I've been trying and reading the documentation, but nothing seems to do the trick as it always searches on the whole string instead of the words. Am i missing something?
Example:
When the user enters 'Mathias Antwerp' he must see all the addresses that contain those words. In the example it must show 1 row which is the second one.
<script>
var addresses = [
{ name: "Frederick Dereave Gentstreet 4 Gent" },
{ name: "Mathias Derian Meilaan 9 Antwerp" },
{ name: "Mathias Hors frelaan 5 Kortrijk" }
];
$(document).ready(SetAutoComplete);
function SetAutoComplete() {
$("#testveld").autoplete(emails,
{
matchContains: "word"
}
);
}
</script>
<input type="text" id="testveld" style='width:300px'/>
Share
Improve this question
asked Aug 17, 2010 at 9:52
MichaelDMichaelD
8,78712 gold badges45 silver badges47 bronze badges
2 Answers
Reset to default 7I altered the code of matchSubset in jquery.autoplete.js which enables the behavior i was looking for.
function matchSubset(s, sub) {
var arraySub=sub.split(" ");
if (!options.matchCase)
s = s.toLowerCase();
var i = s.indexOf(sub);
if (options.matchContains == "word"){
i = s.toLowerCase().search("\\b" + sub.toLowerCase());
}
//addition for split words
if (options.matchContains == "splittedword"){
for(itemindex=0;itemindex<arraySub.length;itemindex++){
i = s.toLowerCase().search(arraySub[itemindex].toLowerCase());
if(i==-1){
break;
}
}
}
if (i == -1) return false;
return i == 0 || options.matchContains;
};
AFAIK, you will have to to do some processing on your own to parse the string into words. You can do this using jquery or if you plan to get the addresses from server side then use some server side language.
本文标签: javascriptJquery autocompletehow to search on words instead of stringStack Overflow
版权声明:本文标题:javascript - Jquery autocomplete, how to search on words instead of string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602134a2665627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论