使用.keyup功能来过滤自动完成 [英] Using .keyup function to filter auto complete
问题描述
在用.keyup过滤我的自动完成。
Am using .keyup to filter my auto complete.
但它只允许我进入的第一位。所以,如果我的数据是苹果
However it only allows me to enter the first digit. so if my data is "Apple"
当我键入A - 它显示了苹果,但我不能输入AP为P消失。
when i type A - it shows Apple but i cannot type "AP" as the "P" disappears.
我期待我能写全字,而不是第一个字母。
I was expecting that i can write the whole word rather than the first letter.
code:
<input id="ac" /> <span></span>
var validOptions = "@Url.Action("SerialProdNumStockSiteAutoComplete", "Ajax")?stocksitenum=LW&model=" + $("#Form_Prod_Num").val();
previousValue = "";
$('#ac').autocomplete({
autoFocus: true,
source: validOptions
}).keyup(function () {
var isValid = false;
for (i in validOptions) {
if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
isValid = true;
}
}
if (!isValid) {
this.value = previousValue
} else {
previousValue = this.value;
}
});
这是工作的时候,我用假数据,但是当我改变这url.action它只是工作的第一个字母,而不是整个单词。
This was working when i used dummy data, but when i changed this to url.action it only worked for the first letter rather than the whole word.
我确实有工作小提琴 - 然而,当我加入我的URL.action 它只接受的第一个字母。 (大约有5000的值)
I do have a fiddle that works - however when i added my "URL.action" it only accepted the first letter. (There are about 5000 values)
推荐答案
在小提琴,你设置的可用选项作为数组:
In the fiddle, you're setting your available options as an array:
var validOptions = ["Bold", "Normal", "Default", "100", "200"]
如果你把你的行动的结果(如直接调用)和替换成的jsfiddle这一点,你会得到(从评论服用):
if you take the result of your action (as called directly) and replace this into the jsfiddle, you'll get (taking from comment):
var validOptions = "B061030029,LL-XXX,"
,或根据在注释引号是否包括(不可能):
or, depending on whether the quotes in the comment are included (unlikely):
var validOptions = ""B061030029,LL-XXX,""
无论哪种方式,这些都是不一样的。
either way, these aren't the same.
使用查看源代码在浏览器中看到什么正在被Url.Action渲染。
Use view-source in the browser to see what is being rendered by the Url.Action.
您可以改变你的行动,返回字符串:
You can change your action to return the string:
"[\"B061030029\", \"LL-XXX\" ]";
那么这将使:
var validOptions = ["B061030029", "LL-XXX"]
相匹配原有的选项。
which matches the original options.
有关整洁,你可以用PartialView做到这一点,仍然继续从行动中的局部视图,而不是一个控制器中的字符串列表和格式返回值。
For neatness, you could do this with a PartialView and still continue to return the values from the action as a string list and format in the partial view rather than the controller.
这篇关于使用.keyup功能来过滤自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!