[英]Populate href in Bootstrap Typeahead dropdown with clickable link
[英]Typeahead and clickable suggestion link
我在search.php中有以下代碼:
<?php
$key=$_GET['key'];
$array = array();
$con=mysql_connect("localhost","user","pass");
$db=mysql_select_db("db",$con);
$query=mysql_query("select * from users where username LIKE '%{$key}%'");
while($row=mysql_fetch_assoc($query))
{
$array[] =$row['code'].$row['id'];
}
echo json_encode($array);
?>
而這個腳本是Javascript
<script src="typeahead.min.js"></script>
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'search.php?key=%QUERY',
href : 'profile.php?id='
});
});
</script>
<script>
$('input.typeahead').on( 'typeahead:selected', function(event, datum) {
window.location = "profile.php?id="
});
</script>
我對js和typeahead的API不太方便,然后我問你:還有另一個函數可以建議url鏈接嗎?
語法是
profile.php?id = $ id
我正在使用window.location但無法在變量數組中傳遞用戶ID謝謝
我知道這篇文章已經很老了,但是,我將為我解決同樣的問題。
您需要使用自定義模板來實現此目的。
$('input.typeahead').typeahead(null, {
displayKey: 'name',
source: restaurants.ttAdapter(),
templates:{
suggestion:function(data) {
return "<a href=""profile.php?id=" + data.id + ">"+ data.name +"</a>"
}
}
});
不要忘記停止事件的下一個傳播:
$('#myFormID').bind('typeahead:select', function(ev, suggestion) {
ev.stopPropagation();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.