尝试让 tag-it 与 AJAX 调用一起使用

2024-01-26

试图得到tag-it http://aehlke.github.com/tag-it/处理 ajax 调用。

到目前为止一切正常。除此之外,我无法通过 ajax 调用分配 tagSource。

在 firebug 中,“数据”正在返回:

["Ruby","Ruby On Rails"]

但当我在输入框中输入内容时它没有显示。

$('.tags ul').tagit({
  itemName: 'question',
  fieldName: 'tags',
  removeConfirmation: true,
  availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"],
  allowSpaces: true,
  // tagSource: ['foo', 'bar']
  tagSource: function() {
    $.ajax({
      url:        "/autocomplete_tags.json",
      dataType:   "json",
      data:       { term: 'ruby' },
      success:    function(data) {
        console.log(data);
        return data;
      }
    });
  }
});

console.log(data)回报["Ruby", "Ruby On Rails"].

我在这里错过了什么吗?还有其他人让这个工作吗?


似乎这个问题已经很长时间没有答案了,我敢打赌你已经找到了解决方案,但对于那些还没有找到解决方案的人,这里是我的答案:

当我从代码中复制时,缩进变得一团糟;)

<input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true">
Tags:<br>
<ul id="mytags"></ul>

<script type="text/javascript">
    jQuery(document).ready(function() {
    jQuery("#mytags").tagit({
        singleField: true,
        singleFieldNode: $('#mySingleField'),
        allowSpaces: true,
        minLength: 2,
        removeConfirmation: true,
        tagSource: function( request, response ) {
            //console.log("1");
            $.ajax({
                url: "search.php", 
                data: { term:request.term },
                dataType: "json",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                            label: item.label+" ("+ item.id +")",
                            value: item.value
                        }
                    }));
                }
            });
        }});
    });

实际上,您可以在一些自动完成 jQuery 示例中找到“search.php”。

<?php
$q = strtolower($_GET["term"]);
if (!$q) return;

$items = array(
    "Great Bittern"=>"Botaurus stellaris",
    "Little Grebe"=>"Tachybaptus ruficollis",
    "Black-necked Grebe"=>"Podiceps nigricollis",
    "Little Bittern"=>"Ixobrychus minutus",
    "Black-crowned Night Heron"=>"Nycticorax nycticorax",
    "Heuglin's Gull"=>"Larus heuglini"
);

function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

        // We first copy each key/value pair into a staging array,
        // formatting each key and value properly as we go.

        // Format the key:
        if( is_numeric($key) ){
            $key = "key_$key";
        }
        $key = "\"".addslashes($key)."\"";

        // Format the value:
        if( is_array( $value )){
            $value = array_to_json( $value );
        } else if( !is_numeric( $value ) || is_string( $value ) ){
            $value = "\"".addslashes($value)."\"";
        }

        // Add to staging array:
        $construct[] = "$key: $value";
    }

    // Then we collapse the staging array into the JSON form:
    $result = "{ " . implode( ", ", $construct ) . " }";

} else { // If the array is a vector (not associative):

    $construct = array();
    foreach( $array as $value ){

        // Format the value:
        if( is_array( $value )){
            $value = array_to_json( $value );
        } else if( !is_numeric( $value ) || is_string( $value ) ){
            $value = "'".addslashes($value)."'";
        }

        // Add to staging array:
        $construct[] = $value;
    }

    // Then we collapse the staging array into the JSON form:
    $result = "[ " . implode( ", ", $construct ) . " ]";
}

return $result;
}

$result = array();
foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
    array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
}
if (count($result) > 11)
    break;
}
echo array_to_json($result);

?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试让 tag-it 与 AJAX 调用一起使用 的相关文章

随机推荐

  • 将带标签的 pandas DF 写入 influxdb

    我有这个 3526 rows x 5 columns DF 其中col0是时间 col1 col3是标签和col4是我的价值 0 1 2 3 4 0 2017 09 29 22 41 51 10 2 95 5 C1195 LF470 SAR
  • JavaScript通过点击改变当前元素的颜色

    我有一个关于仅更改当前元素的颜色的问题 所以我想通过每次点击来更改当前元素的背景颜色 我的问题是 我无法重置前一个元素的背景颜色 例如 我这里有两种背景颜色 黄色 浅蓝色 如果我单击 旧 div 和 新 div 则两个 div 的背景颜色变
  • 将代码字符串转换为 hive 中的 desc

    这里我们有一个连字符的字符串 例如0 1 3 并且长度不固定 hive 中还有一个 DETAIL 表来解释每个代码的含义 DETAIL code desc 0 AAA 1 BBB 2 CCC 3 DDD 现在我们需要一个 hive 查询来将
  • 如何从 DIV 重定向并绕过子锚点 href

    请我需要您的帮助来完成下面的代码 我想向整个 DIV 添加 onclick 事件以重定向到 url 但我无法绕过 DIV 内子锚点中包含的 href 即 目标是点击图像或文本上的任意位置重定向到 google com div div a h
  • 未找到带有 SQLalchemy 数据库的 PostgreSQL

    我使用以下代码使用 sqlalchemy 创建 postgresql 数据库 engine create engine postgresql psycopg2 postgres localhost testData Base metadat
  • 对于自动返回类型,使用哪种更好: decltype 或 std::common_type<>::type (如果可能)?

    作为对我的回答最后一个问题 https stackoverflow com questions 11052842 error with using decltype in c11 creating opaque error message
  • 爱尔兰 Eircode 验证

    我想知道是否有验证爱尔兰 Eircode 格式的最佳实践 到目前为止 我在 JavaScript 中使用 REGEX 的最佳尝试是基于第 11 页上的官方规范here https www eircode ie docs default so
  • Apache Poi excel 删除空白行

    我有一个 3000 行的 Excel 文件 我删除了2000 使用ms excel应用程序 但是当我从代码中调用sheet getLastRowNum 时 它给了我3000 而不是1000 我如何删除空白行 我尝试了来自的代码here ht
  • IIS 7 Cors Ajax Soap 请求

    我遇到了一个无法解决的问题 我有一些 js jquery POST Soap 通过 PHP 请求代码在 Apache 上工作 启用了 Cors 来调用 HTTPS 并且工作正常 我已经迁移到 IIS7 设置响应标头 Access Contr
  • 在 PowerShell 中使用另一个扩展 JSON

    是否有一些简单的方法可以将一个 JSON 文件扩展为另一个文件 并使用 PowerShell 将输出保存到另一个文件 目前我正在尝试编写一个允许我做到这一点的循环函数 但也许有一个更简单的解决方案 迭代转换为 JSON 的属性PSCusto
  • 角度 Ui 树递增是重复的

    我的笨蛋 https plnkr co edit dlG6bJcBP8jaxhVEZ4wq p preview 在 My plunker 中 如果我添加一些子记录 那么如果我删除其中一个子记录 那么如果我添加行 则行会重复 scope ne
  • 将数据从 Android 发送到 PHP 服务器

    我在 android 中有一个应用程序 我想通过 url like mydata php lat 76867 long 87979 我有 php 代码 如果点击此 url 则将数据保存在数据库中 我所不知道的是如何通过我的android手机
  • 在 ASP.NET 中使用线程是否存在任何不明显的危险?

    这是一个兄弟姐妹的问题这个程序员的问题 https softwareengineering stackexchange com questions 13711 servicing background tasks on a large si
  • 设置elasticsearch php客户端的connect_timeout

    我想在我的elasticsearch php 客户端到我的elasticsearch 服务器之间配置一个小的超时 我尝试将一些参数传递给 guzzle 客户端 但似乎这不起作用 这是代码 params array params hosts
  • 如何在appdelegate中关闭viewcontroller?

    我为这样的暂停视图创建launchScreen func applicationWillResignActive application UIApplication let storyboard UIStoryboard name Main
  • Thymeleaf 复选框未传递值

    两个问题 我有用户和注释课程 用户可以有很多笔记 如何通过 Thymeleaf 显示属于用户的每个笔记 id th text u notes id 不起作用 我有一个表格 见图 其中每个用户都有布尔 isUserChecked 值的复选框
  • 以编程方式将视图添加到 LinearLayout 但它们不显示

    我正在尝试填充一个LinearLayout inside simple pdf example xml与 10printed order element2 xml这样我就可以生成一个 PDFListView 这实际上是一个LinearLay
  • 如何使用 JavaScript 从完整路径获取文件名?

    有没有办法可以从完整路径获取最后一个值 基于 符号 Example C Documents and Settings img recycled log jpg 有了这个案例 我只想得到recycled log jpg来自 JavaScrip
  • Zabbix 服务器未运行:显示的信息可能不是最新的

    所以突然之间 在使用它一周后 我在我的电脑上收到一条错误消息zabbix server gui http localhost zabbix http localhost zabbix 错误说 Zabbix server is not run
  • 尝试让 tag-it 与 AJAX 调用一起使用

    试图得到tag it http aehlke github com tag it 处理 ajax 调用 到目前为止一切正常 除此之外 我无法通过 ajax 调用分配 tagSource 在 firebug 中 数据 正在返回 Ruby Ru