所以我试图在jquery中重新创建Facebook标记系统。我不能使用任何插件。我已经有了一些:
<html lang="de">
<head>
<title>Autocomplete</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var triggered = false;
var trigger = "@";
var index = -1;
$("input").autocomplete({
source: [
"AAB",
"AAC",
"CCE",
"CCG",
"IIU",
"IIO"],
search: function () {
if (!triggered) {
return false;
}
},
select: function (event, ui) {
var text = this.value;
var pos = text.lastIndexOf(trigger);
this.value = text.substring(0, pos + trigger.length) + ui.item.value;
triggered = false;
return false;
},
focus: function () {
return false;
},
minLength: 0
}).on("keyup", function () {
var text = this.value;
var len = text.length;
var last;
var query;
if (triggered) {
index = text.lastIndexOf(trigger);
query = text.substring(index + trigger.length);
$(this).autocomplete("search", query);
if (index === -1) {
triggered = false;
}
} else if (len >= trigger.length) {
console.log("ELSE IF")
last = text.substring(len - trigger.length);
triggered = (last === trigger);
}
});
});
</script>
</head>
<body>
<label>Input something here:</label>
<br>
<input type="text" value="" autofocus="true" />
</body>
</html>
所以我遇到了以下问题:标记工作正常,但在我点击@一次后,自动完成功能不会消失。它停止搜索,但仍然显示没有找到结果。我试着解决它如下:
所以我的问题:有没有人知道如何运作?我必须以某种方式重写我的代码吗?
答案 0 :(得分:0)
这有点奇怪......但是在keyup事件中如何:
if(this.value == ""){
triggered = false;
$("input").autocomplete( "close" );
}