【问题标题】:JQuery tokeninput: [Error] TypeError: undefined is not an object (evaluating 'term.replace')JQuery tokeninput: [Error] TypeError: undefined is not an object (evalating 'term.replace')
【发布时间】:2018-07-01 10:37:14
【问题描述】:

我正在使用http://loopj.com/jquery-tokeninput/,因为我需要将一个选择框直接连接到 DB。

我编写了接受 GET 请求的 PHP 脚本,如他的 GitHub 上所述:

<?php
require_once('sondaggio.php');
# Connect to the database
$s = new Sondaggio();
# Perform the query
$query = sprintf("SELECT id, nome from Regioni WHERE nome LIKE '%s%' LIMIT 5", $s->getRealEscapeString($_GET["q"]));
$arr = array();
$arr = $s->readFromDB($query);
# JSON-encode the response
$json_response = json_encode($arr);
# Return the response
echo $json_response;
?>

类 Sondaggio 构造函数:

function __construct(){
    $this->conn = new mysqli($this->host, $this->user, $this->password, $this->database);
    // Check connection
    if ($this->conn->connect_error) {
        exit("Connection failed: " . $this->conn->connect_error);
    }
}

函数 readFromDB($query):

public function readFromDB($query){
    $arr = array();
    $result = $this->conn->query($query);
    if ($result->num_rows > 0){
        while($row = $result->fetch_object()) {
            $arr[] = $row;
        }
    }
    return $arr;
}

一切都经过测试并且运行良好,按照他的文档输出是正确的。但是,当我开始在选择框中输入内容时出现错误:

[Error] TypeError: undefined is not an object (evaluating 'term.replace')
regexp_escape (jquery.tokeninput.js:828)
find_value_and_highlight_term (jquery.tokeninput.js:844:88)
(anonymous function) (jquery.tokeninput.js:899)
each (jquery.min.js:2:11781)
populateDropdown (jquery.tokeninput.js:896)
success (jquery.tokeninput.js:1031)
o (jquery.min.js:2:14739)
fireWith (jquery.min.js:2:15504)
w (jquery.min.js:4:12484)
d (jquery.min.js:4:18320)

所以我遇到了这个错误,我不知道为什么会出现,请帮助我。谢谢

编辑:

var_dump($json_response) (with ...php?q=t) 输出:

string(75) "[{"id":"16","nome":"Toscana\r"},{"id":"17","nome":"Trentino-Alto Adige\r"}]"

【问题讨论】:

  • 嗨@big。你能显示var_dump($json_response);的使用输出吗?
  • 更新了@DamianDziaduch

标签: javascript php jquery-tokeninput


【解决方案1】:

已解决,只是一个错误。 根据http://loopj.com/jquery-tokeninput/ 文档:

您的脚本应以以下格式输出 JSON 搜索结果:

[ {"id":"856","name":"House"}, {"id":"1035","name":"Desperate Housewives"}, ... ]

这里的字段是“id”和“name”。 我的查询改为向数据库询问“id”和“nome”:

$query = sprintf("SELECT id, nome from...

所以我刚刚更新了这样的查询:

$query = sprintf("SELECT id, nome as name from...

现在一切正常!

【讨论】:

  • 我就是这么想的 ;)
猜你喜欢
  • 2020-08-15
  • 2019-01-20
  • 1970-01-01
  • 2016-07-30
  • 2020-08-31
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多