gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:35:38 25 4
gpt4 key购买 nike

试图获取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
}
}));
}
});
}});
});

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

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

?>

关于javascript - 尝试让 tag-it 与 AJAX 调用一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6938802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com