使用jQuery回调(标签/值对)自动完成 [英] Autocomplete using jQuery callback (label/value pair)

查看:81
本文介绍了使用jQuery回调(标签/值对)自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自动完成jQuery,但不了解jQuery UI提供的自动完成函数

I am trying to implement autocomplete jQuery, but am not understanding the autocomplete function that jQuery UI provides.

它使用回调函数,并以标签/值对的形式获取响应.我有一些示例代码,试图将任意标签/值对传递回并显示该选项,但它不起作用.如果有人可以帮我解决这个问题或向我展示一个简单的程序,那就太好了.

It uses a callback function and gets the response as a label/value pair. I have some sample code where I am trying to pass an arbitrary label/value pair back and display that option but it isn't working. If someone can help me out with that or show me a simple program it will be great.

http://jsfiddle.net/kB25J/

HTML:

<html>
    <body>
      Please enter your country name
      <input id ="book" type="text" value="">
    </body>
</html>​

JavaScript:

JavaScript:

$("#book").autocomplete({
    source: function(request, response) {
        alert(request.term);
        response(function() {
            return {
                label: "hi",
                value: "bye"
            }
        });
        alert(reponse);
    }
});

谢谢

推荐答案

发送响应时,传递数组而不是函数.

When sending response, pass an array instead of function.

$(function() {
    $("#book").autocomplete({        
        source: function(request, response) {
            var data = [{
                    label: "hi",
                    value: "bye"
                    }];
            response(data);
        },
        select: function( event, ui ) {
            $( "#book" ).val( ui.item.label); //ui.item is your object from the array
            return false;
        }
    });
});​

这篇关于使用jQuery回调(标签/值对)自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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