jQuery 自动完成问题 - 每个字符在单独的行上


jQuery autocomplete issue - each character on separate line

我正在尝试让自动完成文本框工作。这是我到目前为止的代码:

if ($action == 'find_products')
{
    $dept = $_POST['dept'];
    $stk_adddep = "
        SELECT * FROM stocktake_products WHERE stocktake_id = '{$stocktake_id}' AND is_deli = 0 AND department_description LIKE '{$dept}%' LIMIT 10; ";
     $result = db::c()->query($stk_adddep);
     while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
   $data[] = array(
        'full_name' => $row['product_name'],
        'value' => $row['product_name']);
}
    echo json_encode($data);
    die();
}

Div 以显示文本框:

<input type="text" placeholder="Name" id="customerAutocomplte" />

JS代码:

$('#customerAutocomplte').autocomplete({
            source: function( request, response ) {
            var dept = $('#customerAutocomplte').val();
        $.ajax({
            url: '<?php echo Navigation::gUrl('/users/admin/stocktake_details_nonbcodeditems.php', array('stocktake_id' => $stocktake_id, 'action' => 'find_products'));?>',
            type: 'POST',
            data: {'dept':dept},
                         success: function( data ) {
                         response( $.map( data, function( item ) {
                            return {
                                label: item,
                                value: item
                            }
                        }));
                    }
                });
            },
            autoFocus: true,
            minLength: 0        
          });

PHP部分似乎工作正常,我得到的响应如下:

[{"full_name":"Prince Hubert Cristal","value":"Prince Hubert Cristal"},{"full_name":"Schloer","value" :"Schloer"},{"full_name":"Underberg 20ml","value":"Underberg 20ml"},{"full_name":"敖德萨伏特加20cl" ,"value":"敖德萨伏特加20cl"},{"full_name":"Marula","value":"Marula"},{"full_name":"Maderia Verdelho 15岁","值":"马德里亚·韦尔德略 15 岁"},{"full_name":"马德拉马尔斯梅 15 岁","值":"马德拉岛 Malsmey 15yr old"},{"full_name":"Hennessey 5cl","value":"Hennessey 5cl"},{"full_name":"Jack Daniels(英语:Jack Daniels) 35cl","value":"Jack Daniels35cl"},{"full_name":"马德拉布阿尔10岁","值":"马德拉布阿尔10年 旧"}]

但是,结果在文本框中的显示方式不正确。显示的是整行"值":"杰克丹尼尔斯 35cl",例如,每个字符都是文本框中的单独条目。

尝试在回调函数中使用 item.value:

标签:项目

值值:项目值

$('#customerAutocomplte').autocomplete({
        source: function( request, response ) {
        var dept = $('#customerAutocomplte').val();
    $.ajax({
        url: '<?php echo Navigation::gUrl('/users/admin/stocktake_details_nonbcodeditems.php', array('stocktake_id' => $stocktake_id, 'action' => 'find_products'));?>',
        type: 'POST',
        data: {'dept':dept},
                     success: function( data ) {
                     response( $.map( data, function( item ) {
                        return {
                            label: item.value,
                            value: item.value
                        }
                    }));
                }
            });
        },
        autoFocus: true,
        minLength: 0        
      });