我有一个自动完成文本框需要响应两个事件:
1. 当用户在文本框中输入完毕时(我目前使用“focusout”来假定用户输入完毕。因此,如果用户从文本框中切换到其他地方,表示用户已经输入完毕。)
2. 当用户选择自动完成值列表中的项目时(我使用自动完成的“select”事件来确定)
问题:
当用户在自动完成值列表中选择一个项目时,事件链是这样的:首先调用“focusout”,然后是“select”。当处于“focusout”状态时,我只能访问用户输入的内容,而无法访问用户在自动完成值列表中选择的内容——这正是我所需要的。如何解决这个问题?
复现问题的步骤:
1. 当用户在文本框中输入完毕时(我目前使用“focusout”来假定用户输入完毕。因此,如果用户从文本框中切换到其他地方,表示用户已经输入完毕。)
2. 当用户选择自动完成值列表中的项目时(我使用自动完成的“select”事件来确定)
问题:
当用户在自动完成值列表中选择一个项目时,事件链是这样的:首先调用“focusout”,然后是“select”。当处于“focusout”状态时,我只能访问用户输入的内容,而无法访问用户在自动完成值列表中选择的内容——这正是我所需要的。如何解决这个问题?
复现问题的步骤:
- In the text box, type the letter
a
- Select
ActionScript
from the autocomplete list of values Observe console.debug messages:
focusout event called a select event called ActionScript
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<title>Data Matching</title>
</head>
<body>
<form>
<input id="1" type="text"></input>
<input id="2" type="submit"></input>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$('#1').autocomplete(
{
select: function (event, ui)
{
"use strict";
console.debug('select event called');
console.debug(ui.item.value);
},
source: ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"],
minLength: 1
});
$('#1').focusout(function ()
{
"use strict";
console.debug('focusout event called');
console.debug($(this).attr('value')); // At this point, I need the value that was selected from autocomplete. I only get the value that the user typed, though
});
</script>
</body>
</html>
focusout
事件处理程序。谢谢。 - StackOverflowNewbie