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

试图让tag-it与ajax调用一起使用。

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

在萤火虫中,’数据’正在返回:

["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"]

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

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

当我从我的代码中复制时,缩进了所有搞砸了;)

  Tags:

    和“search.php”你可以在一些自动完成的jQuery示例中找到它。

     "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); ?> 

    看看这个: 如何让tagSource与$ .ajax()一起使用? (来自tag-it的github问题列表)。

    这是一个例子:HTML文件:

                
    • Tag1

    (从[here] [1]获取tag-it.js文件)

    这是PHP文件:

      

    这些答案都没有对我有效,所以我想我会回来发布我的代码确实有效。

     var tagThis = $(".tagit"); tagThis.tagit({ tagSource: function(search, showChoices) { $.ajax({ url: "/tags/search", data: { search: search.term }, dataType: "json", success: function(data) { var assigned = tagThis.tagit("assignedTags"); var filtered = []; for (var i = 0; i < data.length; i++) { if ($.inArray(data[i], assigned) == -1) { filtered.push(data[i]); } } showChoices(filtered); } }); } }); 

    此代码假定URL返回JSON编码的字符串(字符串数组)。 然后,它将过滤掉已在输入中选择的任何标记。 所以它们不会在列表中重复出现。 否则,这适合我。

    感谢其他人让我走上了正确的道路。

    tagSource已被折旧。

    只需使用:

      

    您可以将所有属性添加到此:

      

    你应该删除你的availableTags ,因为你正在重载tagSource ,它被用作自动完成的源。

    也可能是一个错字,但它“ return ”,而不是“ eturn ”。

    编辑:

    我认为问题是你提供给tagSource的函数似乎没有返回任何东西。 但是我的javascript知识似乎在这里结束:/

    我认为您可以从jquery UI覆盖自动完成方法:

     $('.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; } }); }, autocomplete: { delay: 0, minLength: 2, source: this.tagSource() } }); 

    只是补充一下

    假设您的脚本页面看起来像

      

    如果从数据库中获取值,那么您的简单php页面看起来就像

       

    问候