结果限制自动完成jQuery用户界面与功能片 [英] Limit results autocomplete jquery ui with slice function

查看:133
本文介绍了结果限制自动完成jQuery用户界面与功能片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要限制在这个自动完成jQuery UI的脚本结果(最多10个)。我知道我必须使用切片功能,但我不能正确地将其放置在脚本中。在此先感谢您的帮助。

  $(文件)。就绪(函数(){
    变种myArr,该= [];    $阿贾克斯({
        键入:GET,
        网址:events.xml,//改变对服务器上的文件的完整路径
        数据类型:XML,
        成功:parseXml,
        完成:setupAC,
        失败:功能(数据){
            警报(XML文件无法找到);
            }
    });    功能parseXml(XML)
    {
        //发现每一个查询值
        $(XM​​L).find(topevent)。每个(函数()
        {
            //你要创建对象的数组
        变种thisItem = {};
        thisItem ['标签'] = $(本).attr(标签);
        thisItem ['值'] = $(本).attr(值);
        myArr.push(thisItem);        });
    }    功能setupAC(){
    $(#输入searchBoxEv)。自动完成({
    来源:myArr,该,
    的minLength:3,
    选择:函数(事件,UI){
    VAR urlString =htt​​p://mysite.com/+EVENTI /+(ui.item.value)+。html的;
    $(#输入searchBoxEv)VAL(urlString)。
    location.href = urlString;                           }
    });
}
});


解决方案

  $(#汽车)。自动完成({
    来源:函数(请求,响应){
        VAR的结果= $ .ui.autocomplete.filter(myarray中,request.term);        响应(results.slice(0,10));
    }
});

您可以在这里找到更多的文档。

http://jqueryui.com/autocomplete/

I need to limit results (max 10) in this autocomplete jquery ui script. I know I have to use the slice function, but I'm not able to place it correctly inside the script. Thanks in advance for your help.

$(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;

                           }
    });
}
}); 

解决方案

$("#auto").autocomplete({
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(myarray, request.term);

        response(results.slice(0, 10));
    }
});

You could find more documentation here..

http://jqueryui.com/autocomplete/

这篇关于结果限制自动完成jQuery用户界面与功能片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆