输入和点击建议链接

Typeahead and clickable suggestion link

本文关键字:链接 输入      更新时间:2023-09-26

我有这个代码在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和API打字,然后我问你:还有一个功能,提出建议的url链接?

语法是

profile.php吗?id = $ id

我正在使用窗户。但是我不能在变量数组中传递用户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();
    });