我有一個字段「accountname」,它在文本框中使用Jquery autosuggest。這裏當用戶從自動校驗值中選擇並按「Enter Key」填充所選值。 當前我已映射「輸入密鑰」提交表格使用「jquery熱鍵」插件。jquery autosuggest enterkey issue
問題是,只要用戶從autosuggest中選擇了值,他是否按下「Enter」進行選擇。表單正在提交。當用戶在autosuggest下拉列表中按下enterkey時,我不想提交表單。但是當焦點位於自動提示字段時,應該提交表單。
哪個函數需要被覆蓋?
這是我摘錄:
$(document).ready(function() {
var availableTags = [
// abbreviations
{"label":"academy - acad","value":"acad"},{"label":"accident - acc","value":"acc"},{"label":"account - acct","value":"acct"}];
function split(val) {
return val.split(/\s\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#accountName").bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 2,
autoFocus: false,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
//return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(" ");
// added to trim space
$("#accountName").val($.trim($("#accountName").val()));
return false;
}
});
});
請發佈一個有用的代碼示例。 – DG3 2012-02-27 19:12:18
你能顯示你的代碼嗎? select事件應該只在默認情況下填充文本字段,所以除非你提供了自己的select事件,這就是它應該做的。 – itsmequinn 2012-02-27 19:12:31