我正在尝试在我的站点应用程序中实现Typeahead,但是发现了一些问题。我会在搜索框中使用它。预先输入没有显示任何自动完成建议。
我的环境是:
Node.JS
快递
MySQL
index.ejs
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="typeahead.min.js" type="text/javascript"></script>
<script src="../JS/jquery.typeahead.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote: 'http://localhost:3000/search?key=%QUERY',
limit: 10
});
});
</script>
<form method="POST" action="/search">
<label>Search Data</label>
<input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead" type="text" />
</form>
Index.js
router.get('/',function(req,res){
res.render('index');
});
router.post('/search',function(req,res){
var str = {
stringPart:req.body.typeahead
}
db.query('SELECT zipcode, city_name , state_name, party_name, price, address FROM city natural join state natural join party WHERE state_name LIKE "%'+str.stringPart+'%"',function(err, rows, fields) {
if (err) throw err;
res.render('getme', {party: rows});
});
});