2011-01-27 123 views
2

我正在使用jQuery UI自动完成,我试图限制多个结果。基本上,我正在构建一个PM系统,我正在使用自动填充功能来实现。但我试图限制一条消息可以发送到的人数。所以像限制最大选择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. http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/

  3. Facebook style JQuery autocomplete plugin
+0

好了,所以在我的原来的职位,我添加了一些代码。无论如何,我可以删除最新添加到框中?就像我想保留其他名称一样,但我想删除用户添加的最近一个。 http://theproductguy.com/noblecount/noblecount.demo.html在达到允许的最大字符时会执行此类操作。 – jefffan24 2011-01-27 01:02:59