简体   繁体   English

如何拆分搜索jQuery自动完成的字符串?

[英]How to split the string for search jquery auto complete?

In my text field i need to enter more than one string with comma separated values. 在我的文本字段中,我需要输入多个以逗号分隔的字符串。 So when ever i enter string after comma , auto complete suggestions need to trigger. 因此,当我在逗号后输入字符串时,需要触发自动完成建议。

But one value auto complete i have done , but i need your advise more than one suggestions for same text field. 但是我已经完成了一个值自动完成,但是对于同一文本字段,我需要您提供多个建议。

Expectation is : stac(auto complete suggestion),over(suggestion),flo(sugestion) 期望值为:stac(自动完成建议),over(建议),flo(建议)

$(function() {

$("#FeatureName").autocomplete({
    source: function(request, response) {
    $.ajax({
    url: "searchName.jsp",
    type: "POST",
    dataType: "json",
    data: { feature: request.term},
    success: function(data) {
                    var items = data;
                    response(items);
                }

    });
    },
    minLength: 3

    });  

It can be done with only autocomplete but it will be a bit tricky: 只能使用自动完成功能来完成,但是会有些棘手:

Basically what you want is autocomplete to work multiple times on the same input, based on the commas. 基本上,您想要的是自动完成功能,可以基于逗号在同一输入上多次工作。

The autocomplete function of JQUERY is just a ajax call that triggers once you type something on the used input and passes what you have typed to a script that will return the list of suggestions in JSON format. JQUERY的自动完成功能只是一个ajax调用,一旦您在使用的输入上键入内容,然后将您键入的内容传递给脚本,该脚本将以JSON格式返回建议列表,则该触发器会触发。

Ok here's the idea: 好的,这里是个主意:

The parameter you receive in searchName.jsp, let's call it term, can contain multiple words, separated with comma, and you want suggestions for the last one. 您在searchName.jsp中收到的参数(称为术语)可以包含多个单词,以逗号分隔,并且您需要最后一个的建议。

1- Split the term using , . 1-使用,分割术语。

2- Get the last part of the term (the word you want to get suggestions of) and use it to search for suggestions using SQL or any other method you are using to retrieve the list. 2-获取术语的最后一部分(您希望获得建议的单词),并使用它来使用SQL或您用于检索列表的任何其他方法来搜索建议。 If the other words you searched for before this one have to filter the results, just use them in your query! 如果在此之前搜索的其他单词必须过滤结果,则在查询中使用它们!

3- Return that list using JSON format but adding to each return value the part of the term you stripped at the start using split. 3-使用JSON格式返回该列表,但会将您在开始时使用split剥离的术语部分添加到每个返回值中。

I'm sure there's more ways of doing this, but this one should work. 我敢肯定,还有更多的方法可以做到这一点,但是这种方法应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM