dongmangji0950 2016-08-23 07:19
浏览 18

jQuery Tokeninput - 添加新标记 - 如果新标记是任何现有标记的子字符串,则发出问题

Look at following scenario:

enter image description here

I have added a token called "facebook". I want to add a new token called "book". If I write book and press enter, it automatically selects facebook instead of adding new tag called "book".

Is there any solution to achieve this?

  • 写回答

1条回答 默认 最新

  • dongyihao9887 2016-08-23 07:29
    关注

            $(function(){
            var availableTags = [
                                 "actionscript",
                                 "applescript",
                                 "asp",
                                 "basic",
                                 "c",
                                 "c++",
                                 "clojure",
                                 "coldfusion",
                                 "erlang",
                                 "fortran",
                                 "groovy",
                                 "haskell",
                                 "java",
                                 "javascript",
                                 "lisp",
                                 "perl",
                                 "php",
                                 "Python",
                                 "ruby",
                                 "scala",
                                 "scheme"
                               ];
                               $( "#etags" ).autocomplete({
                                 source: availableTags
                               });
        
                               
              $('#tags input').on('focusout',function(){    
                var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,''); // allowed characters
                if(txt) {
                    var flag = false;
                    $(".tag").each(function()
                    {
                            var id = $(this).attr("id");    
                            if(id == txt)
                            {
                                flag = true;
                            }       
                    });
                    if(flag)
                    {
                        alert("Tag already exist!!")
                    } 
                    else
                    {
                        $(this).before('<span class="tag" id ='+txt+' >'+ txt.toLowerCase() +'</span>');
                    }    
                    
                           
                }
                this.value="";
              }).on('keyup',function( e ){
                // if: comma,enter (delimit more keyCodes with | pipe)
                if(/(188|13)/.test(e.which)) $(this).focusout(); 
        
              });
              
              
              $('#tags').on('click','.tag',function(){
                 if(confirm("Really delete this tag?")) $(this).remove(); 
              });
        
            });
            <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
          <link rel="stylesheet" href="/resources/demos/style.css">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    <title>Multiple tags Seperated by comma or Enter by Balram Singh</title>
      
      
      <div id="tags">
    <span class="tag" id="Wordpress">wordpress</span>
    <span class="tag" id="c++">c++</span>
    <span class="tag" id="jquery">jquery</span>
    <input type="text" id="etags"  value="" placeholder="Enter multiple Tags Seperate by comma or Enter " />
      </div>
      

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图