<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>智能填寫</title>
<style type="text/css" > #multiinput {
font-size:22px;
border:1px #000 solid;
font-weight:700;
}
#newInput {
height:24px;
font-size:22px;
border:none;
}
#selection ul {
margin:0;
padding:0;
border:1px solid #000;
width:75px;
border-top:0;
}
#selection li {
list-style:none;
margin:0;
padding:0;
height:24px;
cursor:pointer;
width:75px;
}
#multiinput a:hover {
background-color:#eee;
cursor:text;
}
li.selected {
background-color:#eee;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"><!--
var str = ['張學友','張信哲','劉德華','孫燕姿','周傑倫','張含韻','許飛','許巍','張宇','周星馳','唐朝樂隊','迪克牛仔','鄭伊健','陳小春','汪峰'];
$(function(){
//輸入框獲得焦點時
$("#newInput").focus(function(){
var textStr = ["<ul>"];
for(var i = 0; i < str.length; i++){
textStr.push('<li>' + str[i] + '</li>');
}
textStr.push("</ul>");
textStr = textStr.join('');
$("#selection").show().empty().append(textStr);
})
//輸入框失去焦點時
.blur(function(e){
$("#selection").hide();
})
//按鍵按下時
.keydown(function(e){
if(e.keyCode == 8 && $(this).val().length == 0) {
$(this).prev().remove();
} else if(e.keyCode == 40) {
move(true);
} else if(e.keyCode == 38) {
move(false);
} else if(e.keyCode == 13) {
$('<a>' + $('.selected').text() + ';</a>').insertBefore('#newInput');
$('#newInput').val('').focus();
}
})
//按鍵彈出時,進行搜尋比對。
.keyup(function(e){
if(e.keyCode == 40 || e.keyCode == 8 || e.keyCode == 38)
return;
var textStr = $(this).val();
var reg = new RegExp('(?=[^@]+)[^@]*' + textStr + '[^@]*([email protected]|$)','g');
var arr = str.join("@").match(reg) || [];
textStr = ['<ul>'];
for(var i = 0; i < arr.length; i++) {
textStr.push('<li>' + arr[i] + '</li>');
}
textStr.push('</ul>');
textStr = textStr.join('');
$("#selection").empty().show().append(textStr);
});
//li的背景效果
$("#selection li").live("mouseover",function(){
$(this).siblings().removeClass('selected').end().addClass('selected');
})
//li的選中事件
.live("mousedown",function(){
$('<a>' + $(this).text() + ';</a>').insertBefore("#newInput");
$("#newInput").val('').focus();
});
$("#multiinput a").live('dblclick',function(){
$(this).remove();
});
});
function move(down) {
var selected = $('.selected');
if(down) {
if(selected.length == 0) {
$('#selection li:first').addClass('selected');
} else {
selected.removeClass('selected').next().addClass('selected');
}
} else {
if(selected.length == 0) {
$('#selection li:last').addClass('selected');
} else {
selected.removeClass('selected').prev().addClass('selected');
}
}
}
// --></script>
</head>
<body>
<div id="multiinput">
<a id="t"></a><input id="newInput" type="text" autocomplete="off"/>
</div>
<div>
<div id="selection">
</div>
</div>
</body>
</html>