简体   繁体   中英

jquery autocomplete search input

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
... 
<div id="search">
      <form method="get" action="{% url 'main:home' %}">        
        <input type="text" name="q" placeholder="Поиск по сайту" value="{{ request.GET.q }}"> 
        <input id="input_id" type="submit" value="Найти">                 
      </form>
</div>
... 
<script> 
$('#search input[name="q"]').autocomplete({ 
  'source': '{% url "main:home" %}', 
  'minLength': 2, 
//  'appendTo': "#input:last"   
}); 
</script>

autocomplete removes previous input value图片 if I press autocomplete, the previous value disappears. I tried adding at the end of the line in a commented out version, but this will not work. need to replace the last unfinished word

$('#search input[name="q"]').autocomplete({ 
  'source': '{% url "main:home" %}', 
  'minLength': 2,    
  'appendTo': "#search"   
}).on( "autocompleteselect", function(event, ui) {
  event.preventDefault();
  var words = this.value.split(' ');
  words[words.length - 1] = ui.item.value;
  this.value = words.join(' ');
});

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