【问题标题】:Jquery Autocomplete AJAX call for label value pair JSON dataJquery Autocomplete AJAX 调用标签值对 JSON 数据
【发布时间】:2015-06-06 17:27:45
【问题描述】:

这是我的 Jquery 自动完成代码,它不适用于动态数据。我将 json 数据作为标签:联系人姓名和值:联系人 ID,但此自动完成功能不适用于 AJAX 调用。

$("#autocomplete2").autocomplete({
    //source: data,

    source: function (request, response) {
        $.ajax({
            type: "POST",
            url: "http://localhost/leadata.php",
            dataType: "json",
            data: { q: request.term },
            success: function (data) {
                var transformed = $.map(data, function (el) {
                    return {
                        label: el.label,
                        value: el.value
                    };
                });
                alert(transformed);
                response(transformed);
            },
            error: function () {
                response([]);
            }
        });
    },
    response: function (event, ui) {
        if (ui.content.length === 0) {
            $("#empty-message").text("No results found");
        } else {
            $("#empty-message").empty();
        }
    },
    focus: function (event, ui) {
        event.preventDefault();
        $(this).val(ui.item.label);
    },
    select: function (event, ui) {
        event.preventDefault();
        $(this).val(ui.item.label);
        $("#autocomplete2-value").val(ui.item.value);
        alert(ui.item.value);
        //$('.college').trigger('click');
    }
});

【问题讨论】:

  • 请解释一下“不工作”是什么意思,查看相关的 HTML 也会有所帮助。
  • jQuery UI 自动完成:使用标签-值对
  • 我无法在评论框中粘贴整个代码。有没有其他方法可以共享 html 文件。
  • 如果我使用 AJAX 调用带有标签值的数据自动完成功能不起作用。

标签: jquery autocomplete


【解决方案1】:

这是 test.php 的 PHP 代码

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("databasename", $conn);

    $q = strtolower($_GET["term"]);

$return = array();
    $query = mysql_query("select id,firstname from tablename where firstname like '%$q%'");
    while ($row = mysql_fetch_array($query)) {
    array_push($return,array('label'=>$row['firstname'],'value'=>$row['id']));
}
echo(json_encode($return));
?>

【讨论】:

    【解决方案2】:
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Autocomplete - Remote datasource</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">
      <style>
      .ui-autocomplete-loading {
        background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
      }
      </style>
    <script>
    $(function() {
        $( "#birds" ).autocomplete({
            source: "http://localhost/autocomplete/test.php",
            minLength: 2,
            response: function(event, ui) {
                if (ui.content.length === 0) {
                    $("#empty-message").text("+ Add Contact");
                } else {
                    $("#empty-message").empty();
                }
            },
            focus: function(event, ui) {                    
                event.preventDefault();
                $(this).val(ui.item.label);
            },
            select: function( event, ui ) {
                /*  
                log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
                */
                event.preventDefault();
                //$(this).val(ui.item.label);
    
                $("#birdsval").val(ui.item.label);
                $("#birdsid").val(ui.item.value);
                //$('.college').trigger('click');
                $(this).val('');
    
            },
    
        });
    });
    </script>
    </head>
    <body>
    
    <div class="ui-widget">
        <label for="birds">Birds: </label>
        <input id="birds"><br />
        <input id="birdsid">
        <input id="birdsval">
    </div>
    
    <div id="empty-message"></div>
    
    </body>
    </html>
    

    【讨论】:

    • 嗨,本杰明,如何在此处突出显示术语文本我很困惑。
    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2023-03-22
    • 2012-09-07
    • 2013-10-15
    相关资源
    最近更新 更多