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

时间:2015-06-15 13:05:51

标签: javascript php jquery

我正在尝试使用自动填充文本框。这是我到目前为止的代码:

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部分似乎工作正常,我得到的响应如下:

  

[{&#34; full_name&#34;:&#34; Prince Hubert Cristal&#34;,&#34; value&#34;:&#34; Prince Hubert Cristal&#34;},{&#34 ; FULL_NAME&#34;:&#34;施勒尔&#34;&#34;值&#34;   :&#34; Schloer&#34;},{&#34; full_name&#34;:&#34; Underberg 20ml&#34;,&#34;价值&#34;:&#34; Underberg 20ml&#34;} ,{&#34; full_name&#34;:&#34;敖德萨伏特加20cl&#34;   ,&#34;价值&#34;:&#34;敖德萨伏特加20cl&#34;},{&#34; full_name&#34;:&#34; Marula&#34;,&#34;价值&#34;: &#34; Marula&#34;},{&#34; full_name&#34;:&#34; Maderia Verdelho    15岁&#34;,&#34;价值&#34;:&#34; Maderia Verdelho 15岁&#34;},{&#34; full_name&#34;:&#34;马德拉Malsmey 15岁&#34;, &#34;值&#34;:&#34;马德拉    Malsmey 15岁&#34;},{&#34; full_name&#34;:&#34; Hennessey 5cl&#34;,&#34;价值&#34;:&#34; Hennessey 5cl&#34;},{& #34; full_name&#34;:&#34; Jack Daniels    35cl&#34;,&#34;价值&#34;:&#34; Jack Daniels 35cl&#34;},{&#34; full_name&#34;:&#34; Madeira Bual 10岁&#34;,& #34;价值&#34;:&#34; Madeira Bual 10年    老&#34;}]

但是,结果显示在文本框中的方式不正确。显示的是整条线&#34;值&#34;:&#34; Jack Daniels 35cl&#34;例如,每个字符都是文本框中的单独条目。

1 个答案:

答案 0 :(得分:0)

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

  

label:item.value value: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        
      });