2013-02-09 1 views

답변

2

여기 유일한 차이는 것입니다 귀하의 드롭 다운에 당신에게 여러 값을 제공해야합니다 확인해야하고 extractLast에 연결된 데모와 같은 전화를해야합니다. 여기에 여러 값으로 작동합니다 전체 코드합니다 (source 옵션에 특히주의)이다 :

$("#tags") 
    .on("keydown", function (event) { 
     if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) { 
      event.preventDefault(); 
     } 
    }) 
    .autocomplete({ 
     minLength: 0, 
     source: function (request, response) { 
      var term = $.ui.autocomplete.escapeRegex(extractLast(request.term)) 
       // Create two regular expressions, one to find suggestions starting with the user's input: 
       , startsWithMatcher = new RegExp("^" + term, "i") 
       , startsWith = $.grep(source, function(value) { 
        return startsWithMatcher.test(value.label || value.value || value); 
       }) 
       // ... And another to find suggestions that just contain the user's input: 
       , containsMatcher = new RegExp(term, "i") 
       , contains = $.grep(source, function (value) { 
        return $.inArray(value, startsWith) < 0 && 
         containsMatcher.test(value.label || value.value || value); 
       });    

      // Supply the widget with an array containing the suggestions that start with the user's input, 
      // followed by those that just contain the user's input. 
      response(startsWith.concat(contains)); 
     }, 
     focus: function() { 
      return false; 
     }, 
     select: function (event, ui) { 
      var terms = split(this.value); 
      terms.pop(); 
      terms.push(ui.item.value); 
      terms.push(""); 
      this.value = terms.join(", "); 
      return false; 
     } 
}); 

예 :http://jsfiddle.net/Aa5nK/1/

+0

고마워! 마침내 제가 정확히 무엇을 찾고 있는지 알았습니다. – iDev

0

, 당신은 당신이 당신의 쿼리에서 원하는 내용과 일치하는 결과의 목록을 반환해야 용어가 list_of_terms 배열의 용어의 시작을 동일 입력 한 경우

list_of_terms = {"term0","term1","term2",...}; 

$("#inputsearch").autocomplete({ 
    var term = request.term 
    var list = new Array(); 
    source: function(request, response) { 
     var cnt = 0; 
     $.each(list_of_terms, function(i) { 
      var rSearchTerm = new RegExp('^' + RegExp.quote(term),'i'); 
      if (list_of_terms[i].match(rSearchTerm)) {     
       list[cnt] = list_of_terms[i]; 
       cnt++; 
      } 
     }); 
    response(list); 
    } 
}); 



RegExp.quote = function(str) { 
    return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); 
}; 

것은 내가 괄호를 놓치지 않았다 제공이

+0

이 가고 있습니까? RegExp.quote = function (str) {return (str + '')./(g)를 대체합니다. "\ $ 1"); }; –

+0

이것은 자바 스크립트입니다. 그것은 자바 스크립트에 대한 스크립트 태그에 간다. 당신은 입력 박스형 ID – Tucker

+0

에 의해 입력 상자에 그것을 링크합니다.이 코드는 - RegExp.quote = function (str) {return (str + '')} 대신에 (/? [*?^$ [] \) {} | -])/g, "\ $ 1"); }; 대답의 회색 상자 밖에 있기 때문에. –

관련 문제