【发布时间】:2012-10-08 17:47:30
【问题描述】:
我需要在这个自动完成 jquery ui 脚本中限制结果(最多 10 个)。我知道我必须使用 slice 函数,但我无法将其正确放置在脚本中。提前感谢您的帮助。
$(document).ready(function() {
var myArr = [];
$.ajax({
type: "GET",
url: "events.xml", // change to full path of file on server
dataType: "xml",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert("XML File could not be found");
}
});
function parseXml(xml)
{
//find every query value
$(xml).find("topevent").each(function()
{
//you are going to create an array of objects
var thisItem = {};
thisItem['label'] = $(this).attr("label");
thisItem['value'] = $(this).attr("value");
myArr.push(thisItem);
});
}
function setupAC() {
$("input#searchBoxEv").autocomplete({
source: myArr,
minLength: 3,
select: function(event, ui) {
var urlString = "http://mysite.com/" + "eventi/" + (ui.item.value) + ".html";
$("input#searchBoxEv").val(urlString);
location.href=urlString;
}
});
}
});
【问题讨论】:
-
尝试使用css高度并将溢出设置为滚动
-
我按照您的建议使用了 css(高度和最大高度),但使用巨大的 xml 文件并不是最好的解决方案。
标签: jquery autocomplete limit slice