帮助在php中创建像blogger这样的标签文本区域。 用逗号(,)分隔,并在昏迷后搜索单独的标签。 请帮帮我。
答案 0 :(得分:0)
最后我找到了我想要的东西。这就是我想要的。
<div class="LabelBox">
<label>Label</label>
<textarea id="search_Labels"></textarea>
</div>
<script type="text/javascript">
$(function() {
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#search_Labels").autocomplete({
minLength: 1,
source: "search_label.php",
focus: function() {
// prevent value inserted on focus
return false;
},
open: function (event, ui) {
var acData = $(this).data('ui-autocomplete');
acData
.menu
.element
.find('a')
.each(function () {
var me = $(this);
var keywords = acData.term.split(' ').join('|');
me.html(me.text().replace(new RegExp("(" + keywords + ")", "i"), '<b>$1</b>'));
});
},
select: function(event, ui) {
var term = split(this.value);
// remove the current input
term.pop();
// add the selected item
term.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
term.push("");
this.value = term.join(", ");
return false;
}
});
});
</script>
现在 search_label.php 代码
<?php
//connect to database
$s = $_GET['term']; // Get term for search
// use-full to split label from second label search
$sa = split( ', ', ( trim( mb_substr( $s, 0, ( strrpos( $s, ',' ) ) ) ) ) );
$i = ( substr_count( $s, ',' ) > 0 ) ? 1 : 0;
$s = ltrim( mb_substr( $s, ( strrpos( $s, ',' ) + $i ) ) );
$ar = '';
$i = 0;
if ( $s != '' ) {
$q = "SELECT `label` FROM `table_name` WHERE `label` LIKE '{$s}%' ORDER BY `label`";
$q = mysql_query( $q );
while ( $r = mysql_fetch_array( $q ) ) {
if ( !in_array( $r['label'], $sa ) ) {
$ar[$i] = array( "value" => "{$r['label']}" );
$i++;
}
}
echo json_encode( $ar );
}
?>
示例输出图像