<script src="js/jquery.autocomplete.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#tags input').on('focusout',function(){
var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,'');//allowed characters
if(txt) {
$(this).before('<span class="tag">'+txt.toLowerCase()+'</span>');
}
this.value="";
}).on('keyup',function(e){
// if: comma,enter(delimit more keyCodes with | pipe)
if(/(188|13)/.test(e.which))$(this).focusout();
});
$('#tags').on('click','.tag',function(){
if(confirm("Really delete this tag?"))$(this).remove();
});
});
</script>
<script type="text/javascript">
jQuery(function(){
$("#mytag").autocomplete("list.jsp");
});
</script>
<!-- tsk -->
//htlm input text tag------
<div id="tags">
<input type="text" value="" placeholder="Add a tag" id="mytag" />
</div>
答案 0 :(得分:1)
这是Tag-it和autocomplete的工作示例。检查一下,看看它是如何工作的,一定要采用最新的库并根据您的需求进行定制:
HTML
<ul id="mytags" >
</ul>
JS
$("#mytags").tagit({
autocomplete: { source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON?username=foobar",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", "
+ item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
}, minLength: 2 }
});