duai0935 2016-05-23 11:29
浏览 43
已采纳

自动填充问题

I am trying to display users list in input field with jquery auto-complete function. I facing issue in displaying name and selection update value to field.

My PHP Code:

    include '../_db.php';

    // get what user typed in autocomplete input
    $name = trim($_GET['name']);

    $param = "%{$name}%";
    $query = $conn->prepare('SELECT emp_number, emp_firstname, emp_lastname FROM `hs_hr_employee` WHERE emp_firstname LIKE ? OR emp_lastname LIKE ? ');
    $query->bind_param('ss', $param,$param);
    $query->execute();

    $query->bind_result($emp_number,$emp_firstname,$emp_lastname);
    $a_json = array();
    $a_json_row = array();

    while( $query->fetch() )
    {
        $a_json_row["emp_number"] = $emp_number;
        $a_json_row["fname"] = $emp_firstname;
        $a_json_row["lname"] = $emp_lastname;
        array_push($a_json, $a_json_row);
    }

    $json = json_encode($a_json);
    print $json;

    $query->close();

My JS Code

$(function()
{
    $( "#search-emp" ).autocomplete(
    {
        source: function (request, response)
        {
            var form_data = {
                ajax                : '1',
                name                : $("#search-emp").val(),
                actioncall          : 'search-emp'
            };

            $.ajax({
                type: "POST",
                url: "_ajax.php",
                data: form_data,
                success: function(response)
                {
                    $.each( response, function( key, value )
                    {
                        //alert( key + ": " + value );
                        console.log('element at index ' + key + ' is ' + JSON.parse(value));
                    });
                    //console.log(response);
                },
                dataType: 'json'
            });
        }
    }, {minLength: 3 });
});

Getting Response

[{"emp_number":1,"fname":"Arslan","lname":"Hassan"},{"emp_number":2,"fname":"Muneeb","lname":"Janjua" },{"emp_number":3,"fname":"hr","lname":"user"}]

My HTML code

<form class="form-inline">
    <div class="form-group">
        <label for="exampleInputName2">Employee Name: </label>
        <input id="search-emp" type="text" class="form-control" placeholder="*">
    </div>
    <div class="form-group">
        <label for="exampleInputEmail2">Date Range: </label>
        <input type="text" class="form-control" id="dp-from" placeholder="From">
        <input type="text" class="form-control" id="dp-to" placeholder="To">
    </div>
    <button type="submit" class="btn btn-primary">Genrate Report</button>
</form>
  • 写回答

2条回答 默认 最新

  • douye9175 2016-05-23 14:24
    关注

    The problem it's because jQuery auto-complete wants to have 2 fields named label and value. The content from label will be displayed in the auto-complete.

    Because your server return other name for the keys you have 2 options:

    1. Change the server to return a json like:

      [{"emp_number":1,"fname":"Arslan","lname":"Hassan", "label":"Arslan Hassan", "value": "Arslan Hassan"},....]

    2. Or on the success callback from ajax create an array with this fields (label and value) and pass this array to response() callback.

    You can find more informations here: http://api.jqueryui.com/autocomplete/#option-source

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog