【发布时间】:2015-11-28 17:04:39
【问题描述】:
当我输入列表中不可用的值时,我创建了一个自动完成文本框,它会自动清除结果正确的文本框,但是当我输入列表中的数据并添加一些数据时对它的措辞,文本框的值不清楚,尽管值的最终键不在列表中。
$(document).ready(function() {
var source = ['jQuery', 'Dojo', 'ExtJs', 'Prototype', 'Java', 'Android', 'MySQL', 'PHP'];
$("input#myDropDown").autocomplete({
minLength: 0,
source: source,
autoFocus: true,
scroll: true,
}).focus(function() {
$(this).autocomplete("search", "");
}).live("blur", function(event) {
var autocomplete = $(this).data("autocomplete");
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
autocomplete.widget().children(".ui-menu-item").each(function()
{
var item = $(this).data("item.autocomplete");
if (matcher.test(item.label || item.value || item)) {
//There was a match, lets stop checking
autocomplete.selectedItem = item;
return;
}
});
if (autocomplete.selectedItem) {
autocomplete._trigger("select", event, {
item: autocomplete.selectedItem
});
//there was no match, clear the input
} else {
$(this).val('');
}
});
});
.ui-autocomplete {
position: absolute;
cursor: default;
height: 150px;
overflow-y: scroll;
overflow-x: hidden;
}
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
<input id="myDropDown" type="text" name="myDropDown" />
问题
当我从项目中选择任何一项并为其添加额外的措辞时。 values 键与记录不匹配,但文本框值不清楚。我需要再次聚焦并模糊文本框,文本框只会检测到匹配并清除输入。
【问题讨论】:
-
@DelightedD0D 感谢编辑。
标签: jquery autocomplete textbox