简体   繁体   中英

Autocomplete focus doesn't work correctly

I have an autocomplete input box. I want to display "x" link when I start to write something into input box. But it doesn't work. I don't know what is the mistake. Here are my codes and jsfiddle below.

http://jsfiddle.net/2mu2ht7g/

 <style>
 .icon-removeSearchItem{
   position: absolute;
   margin-left: -16px;
   display:none;
 }
</style>

 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 <script>
  $(function() {
    var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "Ruby",
    "Scala",
    "Scheme"
  ];
  $( "#tags" ).autocomplete({
    source: availableTags
  });
});
</script>

<script>
$( document ).ready(function() {
   $('input').focus(function(){
   if(this.value==this.defaultValue)this.value='';
   var value = $(this).val();
   if($('input').val().trim().length > 0 && value != "Add Comment"){
       $(".icon-removeSearchItem").show();
   }
  });
});
</script>

您的问题尚不清楚。如果您说要显示搜索值的限制数量,则可以使用以下代码:-$(“ #search”).autocomplete({来源:tags.slice(0,limit ),

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM