如何使用jquery自动完成功能获取不在源列表中的文本以进行警报?

时间:2013-02-09 21:16:13

标签: jquery jquery-ui

鉴于此示例,http://jsfiddle.net/CezarisLT/KHqQm/

 <div class="ui-widget">
   <label for="tags">Tags: </label>
   <input id="tags" />
 </div>

 $(function() {
                var availableTags = ["ActionScript", "AppleScript","Asp","BASIC","C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"];
   $( "#tags" ).autocomplete({
                                 source: availableTags,
                                 select: function( event, ui ) {
                                  }
    });
  });

如何在点击回车键后获取不在自动完成源列表中的文本进行提醒?例如,如果我在文本区域中输入“hello world”(不在选项列表中),如何在按下回车键后让hello world报警?

1 个答案:

答案 0 :(得分:0)

添加一个事件处理程序以检查输入中按下的键,并在输入键上,提醒输入值:

$( "#tags" ).on('keyup', function(e) {
      if (e.which == 13) alert(this.value);
  }).autocomplete({
      source: availableTags,
      select: function( event, ui ) {

      }
});