Ajax自动完成自动聚焦不工作与.focus
Ajax AutoComplete autoFocus not working with .Focus
希望有人能帮我解决这个问题。当我单击AutoComplete附加到的输入文本框时,我希望自动完成列表不加过滤地显示(在输入任何字符之前)。在我将autoFocus事件添加到AutoComplete脚本之前,这一切都很好。我需要autoFocus来选择列表中的第一个值。这是因为我想选择一个选项,而不必从列表中选择。这是因为如果用户键入整个字符串并且它存在于列表中,则不会触发select事件(如果未选中),而select事件又不会检索AutoComplete列表中所选字符串的主键(我将其存储在一个隐藏字段中以供以后使用)。
我想指出的是,autoFocus确实在第一个记录上聚焦了大约半秒。
谢谢你事先的建议。
这是我的代码,抱歉,如果它是有点夸张,但认为最好发送原始代码。
var value = null;
function AutoCompleteList(TextBoxName, HiddenFieldName, ValueFieldName, PrimaryKeyFieldName, TableNameName) {
$("input[id*= " + TextBoxName + "]").autocomplete({
autoFocus: true,
source: function (request, response) {
$.ajax({
url: "/Service.asmx/GetListItems",
data: "{ 'max-height':'10', 'prefix': '" + request.term + "', 'ValueField':'" + ValueFieldName + "', 'PrimaryKeyField':'" + PrimaryKeyFieldName + "', 'TableName':'" + TableNameName + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('seperatoor')[0],
val: item.split('seperatoor')[1]
}
}))
},
error: function (response) {
// alert(response.responseText);
},
failure: function (response) {
// alert(response.responseText);
}
});
},
minLength: 0,
select: function (e, i) {
$("input[id*= " + TextBoxName + "]").trigger('change');
$("input[id*= " + HiddenFieldName + "]").val(i.item.val);
value = i.item.val
}
}).focus(function () {
$(this).autocomplete("search");
});
};
我设法解决了我自己的问题,我认为我应该把答案贴在这里,为其他人面临同样的问题。
答案很简单。当我看到自动对焦工作了一会儿就消失了(取消选择),我知道是。focus事件禁用了它,因为它没有。focus事件也能工作。因此,我只是将AutoComplete的autoFocus选项定位在. focus事件中,从而在之后触发。这是修改后的代码。
// jQuery
var value = null;
function AutoCompleteList(TextBoxName, HiddenFieldName, ValueFieldName, PrimaryKeyFieldName, TableNameName) {
$("input[id*= " + TextBoxName + "]").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Service.asmx/GetListItems",
data: "{ 'max-height':'10', 'prefix': '" + request.term + "', 'ValueField':'" + ValueFieldName + "', 'PrimaryKeyField':'" + PrimaryKeyFieldName + "', 'TableName':'" + TableNameName + "'}",
dataType: "json",
type: "POST",
//.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('seperatoor')[0],
val: item.split('seperatoor')[1]
}
}))
},
error: function (response) {
// alert(response.responseText);
},
failure: function (response) {
// alert(response.responseText);
}
});
},
minLength: 0,
select: function (e, i) {
$("input[id*= " + TextBoxName + "]").trigger('change');
$("input[id*= " + HiddenFieldName + "]").val(i.item.val);
value = i.item.val
}
}).focus(function () {
$(this).autocomplete("search");
//I moved the autoFocus option to be triggered here (inside the .Focus event
$("input[id*= " + TextBoxName + "]").autocomplete({
autoFocus: true
})
});
};
相关文章:
- :focus:一起活跃,不在firefox上工作
- 似乎无法获得'.focus()'元素工作.
- jQuery.blur.focus未在测试中工作
- jQuery .focus() 在每个浏览器中的工作方式不同.如何防止这种情况
- Firefox 下的 focus() 无法按预期工作
- ng-focus和ng-blur不起作用,而ng-click在输入元素中工作正常
- 如何使Javascript focus()方法在输入文本框的onBlur事件中工作
- 我创建了一个jquery函数,但focus没有;在引导输入中无法正常工作
- Ajax自动完成自动聚焦不工作与.focus
- Javascript focus()方法不能与Google Apps Script一起工作
- jQuery Focus()或keypress()不工作在IE和Chrome
- 窗口弹出-如何让Window .blur()或Window .focus()在FireFox 4中工作
- 为什么focus()方法不工作
- Angularjs的focus意外地需要async setTimeout来工作
- 即使添加了很多延迟,Focus在IE中也不能工作
- 除了最后一个之外,不能让FOCUS函数工作
- .focus()不能工作,因为动态表还没有加载
- 字段焦点在firefox中不工作,setTimeout(function(){id.focus();},0);
- javascript focus()不能在Firefox和IE上工作
- JavaScript的window.focus()在IE9上不工作