【问题标题】:jQuery autocomplete show hint text in drop downjQuery自动完成在下拉菜单中显示提示文本
【发布时间】:2012-09-02 23:24:55
【问题描述】:

分词器:

http://loopj.com/jquery-tokeninput/

当用户关注输入时显示“输入搜索词”提示。是否可以使用 jQuery UI 的自动完成功能来做到这一点? http://jqueryui.com/demos/autocomplete/

我正在为我的数据使用远程源,但我想可能会分配一个长度为 1 的临时源([“键入搜索词”,“”])来自动完成,直到启动搜索?

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-autocomplete


    【解决方案1】:

    这就是你要找的东西吗:更新演示 http://jsfiddle.net/vWJdT/ *older => * 工作演示 http://jsfiddle.net/MQfEL/

    我猜你要找的是这个:http://loopj.com/jquery-tokeninput/

    希望它适合您的事业:)

    脚本

      <script type='text/javascript' src="https://raw.github.com/loopj/jquery-tokeninput/master/src/jquery.tokeninput.js"></script>
    
    
    
      <link rel="stylesheet" type="text/css" href="http://loopj.com/jquery-tokeninput/styles/token-input.css">
    
    
    
    
      <link rel="stylesheet" type="text/css" href="http://loopj.com/jquery-tokeninput/styles/token-input-facebook.css">
    

    代码

      $(document).ready(function() {
                $("#demo-input-local-custom-formatters").tokenInput([{
                    "first_name": "Tats_innit",
                    "last_name": "Godfrey",
                    "email": "Tats_innit@mit.whatwhatwuthulk.edu",
                    "url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
                },
                {
                    "first_name": "Andre",
                    "last_name": "Jackson",
                    "email": "andre.jackson@yahoo.com",
                    "url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
                },
                {
                    "first_name": "Andre",
                    "last_name": "Jolly",
                    "email": "andre.jolly@uol.com.br",
                    "url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
                },
                {
                    "first_name": "Andre",
                    "last_name": "Henderson",
                    "email": "andre.henderson@globo.com",
                    "url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
                }
              ], {
                  propertyToSearch: "first_name",
                  resultsFormatter: function(item){ return "<li>" + "<img src='" + item.url + "' title='" + item.first_name + " " + item.last_name + "' height='25px' width='25px' />" + "<div style='display: inline-block; padding-left: 10px;'><div class='full_name'>" + item.first_name + " " + item.last_name + "</div><div class='email'>" + item.email + "</div></div></li>" },
                  tokenFormatter: function(item) { return "<li><p>" + item.first_name + " <b style='color: red'>" + item.last_name + "</b></p></li>" },
              });
            });​
    

    工作图像

    顺便说一句,我不是戈弗雷。

    【讨论】:

    • 我不认为这是我所追求的。当用户单击输入框开始输入时,我希望下拉菜单显示提示(“输入搜索词...”)
    • @TylerDeWitt hiya man,更新了我的答案,希望它对你有用! :)
    【解决方案2】:

    您需要在此处更新您的 source 参数,以便您可以自己发出 AJAX 请求。在等待 AJAX 请求返回时,您将向小部件传递一个“加载”项(就像您在问题中建议的那样):

    $("#auto").autocomplete({
        source: function (request, response) {
            // Populate the list with temporary loading item:
            response([loadingItem]);
    
            $.ajax({
                url: "my_remote_source",
                data: request
            }).success(response).error(function () {
                response([]);
            });
        },
        select: function (event, ui) {
            if (ui.item.loading) {
                event.preventDefault();
            }
        },
        focus: function (event, ui) {
            if (ui.item.loading) {
                event.preventDefault();
            }
        }
    });
    

    selectfocus 事件处理程序用于防止“加载”项被单击或聚焦。不幸的是,当您单击加载项时它会消失,但如果它很重要,可以很容易地修复它。

    示例: http://jsfiddle.net/andrewwhitaker/WP29E/1/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      相关资源
      最近更新 更多