duannao1920 2011-01-27 00:20
浏览 130
已采纳

jqueryui autocomplete限制多个选择

I am using the jQuery UI autocomplete and I am attempting to limit the multiple results. Basically I'm building a PM system I am using the autocomplete for the to field. But I'm trying to limit the amount of people a single message can be sent to. So like limit the max selections to 25.

Is there any way to limit this? Also any ideas on a visual indicator that they have reached the max?

 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;
    }
}
  • 写回答

1条回答 默认 最新

  • duandun2136 2011-01-27 00:39
    关注

    That can be achieved real easily by listening events. You could make the color red for example by adding class and removing class to autocomplete. I think you can accomplish this yourself with a little bit of effort.

    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 I also think one of these plugins could be suitable thanks to google:


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

      Looks nice in my opinion:

      Demo tokenizing Autocomplete Plugin

      Click link to view live demo.

    2. http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/

    3. Facebook style JQuery autocomplete plugin
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办