根据此处直接调用自动完成功能正常: -
$('#SearchString').autocomplete({
source: function (request, response) {
$('#Search_EggTimer').css('display', 'inline');
$.ajax({
url: '/KnowledgeBase/Autocomplete',
dataType: "json",
data: {
term: request.term,
ArticleTypeId: $('#ArticleTypeId').val()
},
success: function (data) {
$('#Search_EggTimer').css('display', 'none');
response(data);
}
});
},
select: function (event, ui) {
event.preventDefault();
retrieveselectedID(ui.item.value);
$('#SearchString').autocomplete("close");
},
focus: function (event, ui) {
event.preventDefault();
}
});
但是,如果我尝试将自动填充作为一项功能调用它无法正常工作,它就会在“源:功能(请求,响应)”之前退出'代码行,永远不会被调用。有什么建议吗?
$('#SearchString').on("input", (function (event) {
if ($('#ArticleTypeId').val() == '5') {
var numero = String.fromCharCode(event.keyCode);
var myArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
index = myArray.indexOf(numero);// 1
var longeur = $('#SearchString').val().length;
if (window.getSelection) {
text = window.getSelection().toString();
} if (index >= 0 & text.length > 0) {
} else if (index >= 0 & longeur < 10) {
SearchAutocomplete();
} else { return false; }
}
else {
SearchAutocomplete();
}
}));
// Autocomplete function
function SearchAutocomplete() {
$('#SearchString').autocomplete({
source: function (request, response) {
$('#Search_EggTimer').css('display', 'inline');
$.ajax({
url: '/KnowledgeBase/Autocomplete',
dataType: "json",
data: {
term: request.term,
ArticleTypeId: $('#ArticleTypeId').val()
},
success: function (data) {
$('#Search_EggTimer').css('display', 'none');
response(data);
}
});
},
select: function (event, ui) {
event.preventDefault();
retrieveselectedID(ui.item.value);
$('#SearchString').autocomplete("close");
},
focus: function (event, ui) {
event.preventDefault();
}
});
}
答案 0 :(得分:0)
尝试更改此行,来自&#34;输入&#34;改变&#34;:
$('#SearchString').on("change", (function (event) {
答案 1 :(得分:0)
好的我认为这里发生的事情是自动完成不喜欢成为关键事件的子功能,因此我认为它失去了它的约束力。
我试图做的是有效地做到这一点: -
$('#SearchString').keypress(function (event) {
$('#SearchString').autocomplete({
我试图劫持关键事件以限制字符输入,然后才能达到自动完成功能。
作为一种解决方法,我重新编写了代码来验证而不是限制输入,它不是那么优雅但它可以工作。
$('#SearchString').autocomplete(
{
source: function (request, response) {
if ($('#ArticleTypeId').val() == '5') {
var input = $('#SearchString').val();
var numofCharacters = $('#SearchString').val().length;
var myArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var index = 0;
var checkresult = 0;
while (checkresult != -1 && index < numofCharacters) {
checkresult = myArray.indexOf(input.charAt(index))
index++;
}
if (checkresult == -1) {
alert("Please enter numerical characters only");
return false;
}
}
$('#Search_EggTimer').css('display', 'inline');
$.ajax({
url: '/KnowledgeBase/Autocomplete',
dataType: "json",
data: {
term: request.term,
ArticleTypeId: $('#ArticleTypeId').val()
},
success: function (data) {
$('#Search_EggTimer').css('display', 'none');
response(data);
}
});
},
select: function (event, ui) {
event.preventDefault();
retrieveselectedID(ui.item.value);
$('#SearchString').autocomplete("close");
},
focus: function (event, ui) {
event.preventDefault();
}
});
});
答案 2 :(得分:0)
与我的开发人员朋友讨论了这个问题后,我现在重新编写了代码,但使用了输入限制。这是一种享受。
// Check Keys entered before output to input box
$('#SearchString').keypress(function (event) {
if ($('#ArticleTypeId').val() == '5') {
var number = String.fromCharCode(event.keyCode);
var myArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
index = myArray.indexOf(number);// 1
if (index === -1)
return false;
return true;
}
});
// Autocomplete function
$('#SearchString').autocomplete(
{
source: function (request, response) {
$('#Search_EggTimer').css('display', 'inline');
$.ajax({
url: '/KnowledgeBase/Autocomplete',
dataType: "json",
data: {
term: request.term,
ArticleTypeId: $('#ArticleTypeId').val()
},
success: function (data) {
$('#Search_EggTimer').css('display', 'none');
response(data);
}
});
},
select: function (event, ui) {
event.preventDefault();
retrieveselectedID(ui.item.value);
$('#SearchString').autocomplete("close");
},
focus: function (event, ui) {
event.preventDefault();
}
});
});
希望这有助于将来的任何人。