문제

How I can access the search term entered by the user ?

I've read http://api.jqueryui.com/autocomplete/ and https://github.com/jquery/jquery-ui/blob/master/ui/autocomplete.js , but don't get it...

For example, user types in "sna", and gets a menu with autocomplete candidates:

  • snake
  • snatch
  • snapfish

The user presses KEYDOWN, the autocomplete input field is populated immediately with "snake".

At this moment, I want get the previous, original value of the input field, "sna".

How I can accomplish that ? (programmatically, without pressing the ESC key).

도움이 되었습니까?

해결책

You can use $.ui.autocomplete.escapeRegex(req.term);

var wordlist= [ "about", "above", "across", "after", "against",
                "along", "among", "around", "at", "before", 
                "behind", "below", "beneath", "beside", "between"] ; 

$("#input1").autocomplete({
    // The source option can be an array of terms.  In this case, if
    // the typed characters appear in any position in a term, then the
    // term is included in the autocomplete list.
    // The source option can also be a function that performs the search,
    // and calls a response function with the matched entries.
    source: function(req, responseFn) {
        var re = $.ui.autocomplete.escapeRegex(req.term);
        alert(re); //THIS IS WHAT YOU WANT
        var matcher = new RegExp( "^" + re, "i" );
        var a = $.grep( wordlist, function(item,index){
            return matcher.test(item);
        });
        responseFn( a );
    }
});

Fiddle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top