2011-01-27 3 views
2

jQuery UI 자동 완성 기능을 사용 중이고 여러 결과를 제한하려고합니다. 기본적으로 필자는 PM 시스템을 구축 중이며 to 필드에 자동 완성 기능을 사용하고 있습니다. 하지만 한 메시지를 보낼 수있는 사람의 수를 제한하려고합니다. 그래서 최대 선택 수를 25로 제한하십시오.jqueryui 자동 완성 제한 다중 선택

제한 방법이 있습니까? 또한 그들이 최대에 도달 한 시각적 표시기에 대한 아이디어가 있습니까?

select: function(event, ui){ 
    var terms = split(this.value); 
    if(terms.length <= 2) 
    { 
     // remove the current input 
     terms.pop(); 
     // add the selected item 
     terms.push(ui.item.value); 
     // add placeholder to get the comma-and-space at the end 
     terms.push(""); 
     this.value = terms.join(", "); 
     return false; 
    } 
    else 
    { 
     $(this).effect("highlight", {}, 1000); 
     $(this).addClass("red"); 
     $("#warnings").html("<span style='color:red;'>Max people reached</span>"); 
     return false; 
    } 
} 

답변

4

이것은 events을 통해 쉽게 실현할 수 있습니다. 예를 들어 adding class으로 빨간색을 표시하고 클래스를 자동 완성으로 제거 할 수 있습니다. 나는 당신이 약간의 노력으로 이것을 스스로 달성 할 수 있다고 생각합니다.

select: function(event, ui) { 
    var terms = split(this.value); 
    if(terms.length <= 2) { 
     // remove the current input 
     terms.pop(); 
     // add the selected item 
     terms.push(ui.item.value); 
     // add placeholder to get the comma-and-space at the end 
     terms.push(""); 
     this.value = terms.join(", "); 
     return false; 
    } else { 
     var last = terms.pop(); 
     $(this).val(this.value.substr(0, this.value.length - last.length - 2)); // removes text from input 
     $(this).effect("highlight", {}, 1000); 
     $(this).addClass("red"); 
     $("#warnings").html("<span style='color:red;'>Max people reached</span>"); 
     return false; 
    } 
} 

P.S 또한 google 이러한 플러그인 중 하나가 적절한 감사가 될 수 있다고 생각 :

Demo tokenizing Autocomplete Plugin

:


  1. https://github.com/loopj/jQuery-Tokenizing-Autocomplete-Plugin

    내 의견에 좋은 보이는

    live demo을 보려면 링크를 클릭하십시오.

  2. 나는 몇 가지 코드를 추가 한 내 원래의 게시물 최대 있도록 확인
  3. Facebook style JQuery autocomplete plugin
+0

  • http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/. 어쨌든 상자에 최신 추가 기능을 제거 할 수 있습니까? 다른 이름을 유지하려는 것처럼 사용자가 추가 한 가장 최근의 이름을 제거하려고합니다. http://theproductguy.com/noblecount/noblecount.demo.html 허용되는 최대 문자 수에 도달하면 이러한 종류의 작업이 수행됩니다. – jefffan24

  • +0

    코드에서와 같이'split (term) .pop()'을 사용할 수 없습니까? 추신 : 어쩌면 당신은 이미 당신의 기능을 가지고 있기 때문에 또한 꽤 (페이스 북처럼) => https://github.com/loopj/jQuery-Tokenizing-Autocomplete-Plugin – Alfred

    +0

    내 스 니펫을 업데이트했습니다. 네가 원하는 것 같아. – Alfred

    관련 문제