需要的js,css(远程的链接复制直接使用,下载请自行复制)
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
html
<div class="ui-widget"> <label for="tags">领导:</label> <input id="tags"/> </div> <div class="ui-widget"> <label for="tagsub">抄送:</label> <input id="tagsub"/> </div>
js
<script> $(function() { var autoVal = []; //数据格式最好以一个符号分割,用于下面open函数分割数组用 var availableTags = [ "王哥;", "居;", "tomy;", "陈彪;", "陈涛;", "冯涛;", "谭茹丹;", "老司机;", "威哥;", "周洁;", "紫阳;", "教练;", "维克多;", "春哥;", "钱老板;", "俊杰;", "国强;", "PHP;", "Python;", "Ruby;", "Scala;", "Scheme;" ]; $("#tags").autocomplete({ source: availableTags }); $("#tagsub").autocomplete({ source: function( request, response ) { var a=$.ui.autocomplete.escapeRegex( request.term ); var newdata= a.split(';') var matcher = new RegExp( "^" + newdata[newdata.length-1], "i" ); response($.grep( availableTags, function( item ){ return matcher.test(item); }) ); }, //关闭函数,用于修改最终input的值 close: function( event, ui ) { var val = $('#tagsub').val(); autoVal.push(val); $('#tagsub').val(autoVal.join('')); console.log('close:' + autoVal.join('')); }, //打开列表函数,需要保存删除后的数组 open:function( event, ui ) { var val = $('#tagsub').val(); var array=[]; array=val.split(";"); console.log('111:' + array.toString); if(array.toString().substring(array.toString().length-1,array.toString().length)==","){ autoVal=[]; }else { for (i = 0; i < array.length; i++) { array[i] = array[i] + ";"; } if (array.length != 1) { array.splice(array.length - 1, 1); autoVal = array; } } } }); }); </script>