简体   繁体   中英

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)

$(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.

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.

1- Split the term using , .

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. 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.

I'm sure there's more ways of doing this, but this one should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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