0

我一直在尝试在我的项目中使用 typeahead.js。我需要来自我的 mysql 数据库的数据。但是没有任何建议。我几乎尝试了一切。这是我的代码:

HTML 文件:

    <html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script src="js/typeahead.js"></script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Companies: </label>
  <input id="tags"/>
</div>

 <script>
$('#tags').typeahead({                                
  name: 'tags',                                                          
  prefetch: 'file.json'                                                            
});
</script>
</body>
</html>

文件.json

    [ 
{ 
    "value": "Maini Optics",
    "tokens": ["Maini","Optics"]
},
{
    "value": "Prakash Eyewear",
    "tokens": ["Prakash","Eyewear"]
},
{
    "value": "Eternity Lifestyles",
    "tokens": ["Eternity","Lifestyles"]
},
{
    "value": "Aeurole Inspecs",
    "tokens": ["Aeurole","Inspecs"]
},
{
    "value": "Dynamic Eyewear",
    "tokens": ["Dynamic","Eyewear"]
},
{
    "value": "abc",
    "tokens": ["abc"]
},
{
    "value": "Test",
    "tokens": ["Test"]
}
]

请帮忙...

4

2 回答 2

0

PHP(数组中的单个值,即: $values = array('Maini Optics', 'Prakash Eyewear') ):

$typeahead_data = implode("','", $values);

HTML:

<input type="text" name="name" id="name_typeahead" autocomplete="off" />
<button type="submit" class="btn btn-primary"><i class="icon-search icon-white"></i>&nbsp;Search</button>

JS

$("#name_typeahead").typeahead({
    source: ['<?=$typeahead_data;?>'],
    items: 20
});
于 2013-10-16T13:21:21.850 回答
0

你必须提供姓名

{ 
    "value": "Maini Optics",
    "name": "Maini Optics",
    "tokens": ["Maini","Optics"]
}
于 2013-11-13T06:55:04.673 回答