Jquery自动填充文本区域

时间:2012-03-30 05:43:37

标签: jquery-ui autocomplete

我正在为文本区域使用jquery自动完成功能。我希望每按一次“Space-Bar”就会调用自动完成功能。它只在第一次工作正常。有人可以帮我解决这个问题吗?**

我使用了以下代码:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"      type="text/javascript">    </script>
        <script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
        <script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.2.js"  type="text/javascript"></script>
        <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/minified/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script>
function auto_comp(flag1)
{

$(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",
        "{ }"

    ];
if(flag1 == 1)
{

    $( "#tags" ).autocomplete({

        source: availableTags
    });

}
});
}
</script>

   

<div class="demo">

<div class="ui-widget">
<textarea id="tags" name="get" rows="5" cols="31"></textarea>

<script>
  var charfield=document.getElementById("tags");

 var flag=0;
 charfield.onkeypress=function(e){
  var e=window.event || e;
  var keyunicode=e.charCode || e.keyCode;
   if(keyunicode == '97'|| keyunicode == '65')
  {
   flag=1;
  auto_comp(flag);

 }
document.getElementById("unicode").value=keyunicode;
}

  </script>
</body></html>

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

jQuery autocomplete是一个小部件,因此每次需要它时都不需要调用它。你可以做的是:

$( "#tags" ).autocomplete({
    source: availableTags
}).bind( "autocompletesearch", function(event, ui) {
    return !(event.keyCode == '97'|| event.keyCode == '65');
});

答案 2 :(得分:0)

我认为你正在寻找这个:

Jquery Ui Auto-complete multiple values (Click here)