我是jquery&的新手自动完成,我正在尝试学习它们。以下工作正如我所料,除非有人输入内容并点击ENTER。然后文本框变为空白。我怎么压制这个?我希望它触发现有事件之一 - 选择或更改。谢谢,
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
a.test { font-weight: bold; }
</style>
</head>
<body>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css">
<link rel="stylesheet" type="text/css" href="css/autocomplete.css">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<script>
$(document).ready(function(){
//attach autocomplete
$("#suggest4").autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("friends.php?callback=?", req, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
});
},
//define select handler
select: function(e, ui) {
$("#spill").html("change "+ui.item.value+" "+e.type);
},
//define select handler
change: function(e) {
$("#spill").html("change "+$(this).val()+e.type);
}
});
});
</script>
<div id="content">
<form>
<p>
<label>Multiple Birds (local):</label>
<input type="text" id="suggest4"></input>
</p>
</form>
</div>
<div id="spill">
</div>
</body>
</html>
答案 0 :(得分:0)
您需要绑定到输入按键事件,查找输入然后执行您想要的操作:
$('#suggest4').keypress(function(e) {
//catch only enter
if(e.keyCode == 13) {
e.preventDefault();
//put your stuff in here
}
});