简体   繁体   English

从Bootstrap typeahead和JSON创建可点击的结果列表

[英]Making clickable result list from Bootstrap typeahead and JSON

I want to make the result list for my Bootstrap typeahead list clickable and if the user clicks on any of the items in the dropdown list it will be redirected to the url [on my site, not external links] of the selected item. 我想让我的Bootstrap预先输入列表的结果列表可以点击,如果用户点击下拉列表中的任何项目,它将被重定向到所选项目的[我的网站上的网址,而不是外部链接]。 I made my changes regarding this Stackoverflow topic: jquery autocomplete json and clickable link through 我对此Stackoverflow主题进行了更改: jquery autocomplete json和可点击链接

The problem is, that I'm not into JS and Jquery and I can't tell why I get this error (Firefox Firebug Console output). 问题是,我没有进入JS和Jquery,我不知道为什么我会收到此错误(Firefox Firebug控制台输出)。 I get this error everytime I enter any letter in my input textbox: 每次我在输入文本框中输入任何字母时都会收到此错误:

TypeError: it.toLowerCase is not a function     bootstrap3-typeahead.min.js (1. line, 3920. column)

I see that the results of my PHP seems okay, so it must be something in the jQuery statement... 我看到我的PHP的结果似乎没问题,所以它必须是jQuery语句中的内容......

This is my result from the PHP: 这是我的PHP结果:

[{"name":"TEXT-ONE","url":"\/textone-postfix"},{"name":"TEXT-TWO","url":"\/texttwo-postfix"},{"name":"TEXT-THREE"
,"url":"\/textthree-postfix"}]

This is my JQuery code: 这是我的JQuery代码:

$(document).ready(function() {
$(function() {
  $('#namesearch').typeahead({
    source: function(request, response) {
      $.ajax({
        url: '/functions/search-autocomplete.php',
        type: 'POST',
        dataType: 'JSON',
        data: 'query=' + request,
        success: function(data) {
          response($.map(data, function(item) {
            return {
                url: item.url,
                value: item.name
            }
          }))
        }
      })
    },
    select: function( event, ui ) {
      window.location.href = ui.item.url;
    }
  });
});
});

This is my PHP code: 这是我的PHP代码:

<?php

require_once('../config/config.php');
require_once('../functions/functions.php');
require_once('../config/db_connect.php');

$query = 'SELECT name_desc FROM tbl_name ';

if(isset($_POST['query'])){
  $query .= ' WHERE LOWER(name_desc) LIKE LOWER("%'.$_POST['query'].'%")';
}

$return = array();

if($result = mysqli_query($conn, $query)){
  // fetch object array
  while($row = mysqli_fetch_row($result)) {
    $array = array("name" => $row[0], "url" => "/" . normalize_name($row[0])."-some-url-postfix");
    $return[] = $array;
  }
  // free result set
  $result->close();
}

// close connection
$conn->close();

$json = json_encode($return);
print_r($json);

?>

Can someone please help me what could be the problem here? 有人可以帮我解决这里可能出现的问题吗?

Thank you very much! 非常感谢你!

The problem was that the displayText wasn't defined: 问题是没有定义displayText:

$(document).ready(function() {
$(function() {
  $('#namesearch').typeahead({
    source: function(request, response) {
      $.ajax({
        url: '/functions/search-autocomplete.php',
        type: 'POST',
        dataType: 'JSON',
        data: 'query=' + request,
        success: function(data) {
          response($.map(data, function(item) {
            return {
                url: item.url,
                value: item.name
            }
          }))
        }
      })
    },
    displayText: function(item) {
        return item.value
    },
    select: function( event, ui ) {
      window.location.href = ui.item.url;
    }
  });
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM