1
我有一个jQuery UI自动完成列表,并希望它打开和关闭在点击输入字段或图标。 当我点击页面上的其他任何地方时,自动完成功能会自动关闭。这很好。但是:当我点击我的图标时,我不想让这种闭合自动操作触发。目前,当列表打开并点击图标时,列表首先关闭(由于自动化),然后立即重新打开。jQuery:自动完成,不要关闭按钮点击
我可以以某种方式防止仅在点击图标时自动关闭吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>...</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<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/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<div class=container>
<span id="regionGlyph" class="glyphicon glyphicon-chevron-right "></span>
<input id="regionIn" type="text" class="form-control">
</div>
<script>
$(document).ready(function() {
var availableTags =
["Aach (BW)",
"Aachen (NW)",
"Aalen (BW)",
"Abenberg (BY)",
"Abensberg (BY)",
"Achern (BW)",
"Achim (NI)",
"Adelsheim (BW)",
"Adenau (RP)",
"Adorf/Vogtl. (SN)",
"Ahaus (NW)",
"Ahlen (NW)",
"Ahrensburg (SH)",
"Aichach (BY)",
"Aichtal (BW)",
"Aken (Elbe) (ST)",
"Albstadt (BW)",
"Alfeld (Leine) (NI)",
"Allendorf (Lumda) (HE)",
"Allstedt (ST)",
"Alpirsbach (BW)",
"Arnsberg (NW)"];
var closed=true;
$("#regionIn").autocomplete({
source: availableTags,
minLength: 0,
close: function(){
closed=true;
},
search: function(){
closed=false;
}
})
.click(function() {
if (closed){
$(this).autocomplete('search');
} else {
$(this).autocomplete('close');
};
});
$("#regionGlyph").click(function(){
$("#regionIn").trigger("click");
});
});
</script>
</body>
</html>
这里是jsfiddle。
你的意思是这样[这](https://jsfiddle.net/6e2mtr69/2/)? – Huelfe
看起来正确,但似乎与我的代码具有完全相同的问题。另外,它甚至没有在我的浏览器中打开自动完成功能(????)。 – PeMa
哪个问题?这把小提琴适合我。如果您点击图标并关闭自动完成,则会打开窗口。如果窗口打开,并且您单击该图标,则不会发生任何事情。 – Huelfe