javascript - jquery自动完成搜索输入
问题描述
<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>
如果我按下自动完成,自动完成会删除以前的输入值 ,以前的值会消失。我尝试在注释掉的版本中添加行尾,但这不起作用。需要替换最后一个未完成的单词
解决方案
$('#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(' ');
});
推荐阅读
- fortran - 任务完成后 MPI 发送和接收。甚至可能吗?
- algorithm - 所需的最少操作次数
- c++ - 如何从 boost 1.71 中的“basic_socket”获取“io_context”?
- java - java.util.Base64 未找到,无法使用 android.util.Base64,因为类是在 Android 之外使用的依赖项
- google-cloud-platform - 谷歌云构建无法找到 git 路径
- c# - 当页面实际上是一种方法时,如何等待页面加载?
- asp.net - 响应状态码不表示成功:429 (Too Many Requests)
- html - 缩小间距
和 - python - 从 Excel 生成 JSON 文件以在 Python 中读取
- tensorflow - 如何创建具有子模型条件评估的组合 tf.keras 模型