2012-03-14 38 views
1

這裏是我的問題在下面的JS小提琴總結: http://jsfiddle.net/sidou/3R5B2/jQuery的口音與多個值不敏感的自動完成

我需要與多個值的自動完成字段(這是正確的連接腳本的第一部分完成),但我也希望它在獲取與輸入字符串相比較的自動填充建議時(與它在附加腳本的第二部分中的工作原理完全相同)時不區分變音。

如何合併兩種行爲?或者換句話說,如何簡單地使第一個自動補全字段變得不重要,同時保持多值選擇特徵。

您可以通過鍵入單詞「食堂」

感謝

回答

3

這裏試試吧,我固定它給你:http://jsfiddle.net/cps7/3R5B2/7/。第二個輸入按照你的意願行事。

$(function() { 
 
    var keywordz = [ 
 
    "Caféteria", 
 
    "AppleScript", 
 
    "Asp", 
 
    "BASIC", 
 
    "C", 
 
    "C++", 
 
    "Clojure", 
 
    "COBOL", 
 
    "ColdFusion", 
 
    "Erlang", 
 
    "Fortran", 
 
    "Groovy", 
 
    "Haskell", 
 
    "Java", 
 
    "JavaScript", 
 
    "Lisp", 
 
    "Perl", 
 
    "PHP", 
 
    "Python", 
 
    "Ruby", 
 
    "Scala", 
 
    "Scheme" 
 
    ]; 
 

 
    //////////////FIRST AUTOCOMPLETE WITHOUT ACCENT CHECK/////////////// 
 

 
    function split(val) { 
 
    return val.split(/,\s*/); 
 
    } 
 

 
    function extractLast(term) { 
 
    return split(term).pop(); 
 
    } 
 

 
    $('#keywords') 
 
    .keydown(function(e) { 
 
     if (e.keyCode === $.ui.keyCode.TAB && 
 
     $(this).data("autocomplete").menu.active) { 
 
     e.preventDefault(); 
 
     } 
 
     if (e.which == 13) { 
 
     e.preventDefault(); 
 
     } 
 

 
     $('#keywords').autocomplete({ 
 

 
     minLength: 1, 
 
     autoFocus: true, 
 
     source: function(request, response) { 
 
      // delegate back to autocomplete, but extract the last term 
 
      response($.ui.autocomplete.filter(
 
      keywordz, extractLast(request.term))); 
 
     }, 
 
     focus: function() { 
 
      // prevent value inserted on focus 
 
      return false; 
 
     }, 
 
     select: function(event, ui) { 
 
      var terms = split(this.value); 
 
      // 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; 
 
     } 
 

 
     }) 
 
    }) 
 
    //////////////END OF FIRST AUTOCOMPLETE WITHOUT ACCENT CHECK////////// 
 

 
    //////////////SECOND AUTOCOMPLETE WITH ACCENT CHECK/////////////// 
 

 
    var accentMap = { 
 
    "à": "a", 
 
    "â": "a", 
 
    "é": "e", 
 
    "è": "e", 
 
    "ê": "e", 
 
    "ë": "e", 
 
    "ï": "i", 
 
    "î": "i", 
 
    "ô": "o", 
 
    "ö": "o", 
 
    "û": "u", 
 
    "ù": "u" 
 
    }; 
 
    var normalize = function(term) { 
 
    var ret = ""; 
 
    for (var i = 0; i < term.length; i++) { 
 
     ret += accentMap[term.charAt(i)] || term.charAt(i); 
 
    } 
 
    return ret; 
 
    }; 
 

 

 
    $('#keywords2') 
 
    .keydown(function(e) { 
 
     if (e.keyCode === $.ui.keyCode.TAB && 
 
     $(this).data("autocomplete").menu.active) { 
 
     e.preventDefault(); 
 
     } 
 
     if (e.which == 13) { 
 
     e.preventDefault(); 
 
     } 
 

 
     $('#keywords2').autocomplete({ 
 

 
     minLength: 1, 
 
     autoFocus: true, 
 
     //with accent check   
 
     source: function(request, response) { 
 
      var matcher = new RegExp($.ui.autocomplete.escapeRegex(extractLast(request.term)), "i"); 
 
      response($.grep(keywordz, function(value) { 
 
      value = value.truc || value.value || value; 
 
      return matcher.test(value) || matcher.test(normalize(value)); 
 
      })); 
 
     }, 
 
     focus: function() { 
 
      // prevent value inserted on focus 
 
      return false; 
 
     }, 
 
     select: function(event, ui) { 
 
      var terms = split(this.value); 
 
      // 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; 
 
     } 
 

 
     }) 
 
    }) 
 
    //////////////END OF SECOND AUTOCOMPLETE WITH ACCENT CHECK////////// 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
multi values autocomplete: <input type="text" id="keywords" size="50"> 
 
<br/><br/> accent insensitive autocomplete: <input type="text" id="keywords2" size="30">

+0

YESSS它的偉大工程:),我知道你做了什麼。非常感謝你是冠軍:) – Sidou 2012-03-15 19:30:00