JavaScript实现带自动提示的文本框效果代码
程序员文章站
2023-12-25 17:35:27
示例一:直接编写ajax 实现。 客户端: 复制代码 代码如下:
示例一:直接编写ajax 实现。
客户端:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<title>ajax实现自动提示的文本框</title>
<style>
<!--
body{
font-family:arial, helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* 用户输入框的样式 */
font-family:arial, helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* 提示框div块的样式 */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:arial, helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* 显示提示框的边框 */
border:1px solid #004a7e;
}
#popup.hide{
/* 隐藏提示框的边框 */
border:none;
}
/* 提示框的样式风格 */
ul{
list-style:none;
margin:0px; padding:0px;
}
li.mouseover{
background-color:#004a7e;
color:#ffffff;
}
li.mouseout{
background-color:#ffffff;
color:#004a7e;
}
-->
</style>
<script language="javascript">
var oinputfield; //考虑到很多函数中都要使用
var opopdiv; //因此采用全局变量的形式
var ocolorsul;
var xmlhttp;
function createxmlhttprequest(){
if(window.activexobject)
xmlhttp = new activexobject("microsoft.xmlhttp");
else if(window.xmlhttprequest)
xmlhttp = new xmlhttprequest();
}
function initvars(){
//初始化变量
oinputfield = document.forms["myform1"].colors;
opopdiv = document.getelementbyid("popup");
ocolorsul = document.getelementbyid("colors_ul");
}
function clearcolors(){
//清除提示内容
for(var i=ocolorsul.childnodes.length-1;i>=0;i--)
ocolorsul.removechild(ocolorsul.childnodes[i]);
opopdiv.classname = "hide";
}
function setcolors(the_colors){
//显示提示框,传入的参数即为匹配出来的结果组成的数组
clearcolors(); //每输入一个字母就先清除原先的提示,再继续
opopdiv.classname = "show";
var oli;
for(var i=0;i<the_colors.length;i++){
//将匹配的提示结果逐一显示给用户
oli = document.createelement("li");
ocolorsul.appendchild(oli);
oli.appendchild(document.createtextnode(the_colors[i]));
oli.onmouseover = function(){
this.classname = "mouseover"; //鼠标经过时高亮
}
oli.onmouseout = function(){
this.classname = "mouseout"; //离开时恢复原样
}
oli.onclick = function(){
//用户点击某个匹配项时,设置输入框为该项的值
oinputfield.value = this.firstchild.nodevalue;
clearcolors(); //同时清除提示框
}
}
}
function findcolors(){
initvars(); //初始化变量
if(oinputfield.value.length > 0){
createxmlhttprequest(); //将用户输入发送给服务器
var surl = "9-10.aspx?scolor=" + oinputfield.value + "×tamp=" + new date().gettime();
xmlhttp.open("get",surl,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readystate == 4 && xmlhttp.status == 200){
var aresult = new array();
if(xmlhttp.responsetext.length){
aresult = xmlhttp.responsetext.split(",");
setcolors(aresult); //显示服务器结果
}
else
clearcolors();
}
}
xmlhttp.send(null);
}
else
clearcolors(); //无输入时清除提示框(例如用户按del键)
}
</script>
</head>
<body>
<form method="post" name="myform1">
color: <input type="text" name="colors" id="colors" onkeyup="findcolors();" />
</form>
<div id="popup">
<ul id="colors_ul"></ul>
</div>
</body>
</html>
服务器端(9-10.aspx ):
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<%@ import namespace="system.data" %>
<%
response.cachecontrol = "no-cache";
response.addheader("pragma","no-cache");
string sinput = request["scolor"].trim();
if(sinput.length == 0)
return;
string sresult = "";
string[] acolors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass","bronze","brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue","feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod","golenrod","gostwhite","gray","green","greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray","snow","springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
for(int i=0;i<acolors.length;i++){
if(acolors[i].indexof(sinput) == 0)
sresult += acolors[i] + ",";
}
if(sresult.length>0) //如果有匹配项
sresult = sresult.substring(0,sresult.length-1); //去掉最后的“,”号
response.write(sresult);
%>
示例二:使用jquery 实现。
客户端:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<title>jquery实现自动提示的文本框</title>
<style>
<!--
body{
font-family:arial, helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* 用户输入框的样式 */
font-family:arial, helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* 提示框div块的样式 */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:arial, helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* 显示提示框的边框 */
border:1px solid #004a7e;
}
/* 提示框的样式风格 */
ul{
list-style:none;
margin:0px; padding:0px;
color:#004a7e;
}
li.mouseover{
background-color:#004a7e;
color:#ffffff;
}
-->
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
var oinputfield; //考虑到很多函数中都要使用
var opopdiv; //因此采用全局变量的形式
var ocolorsul;
function initvars(){
//初始化变量
oinputfield = $("#colors");
opopdiv = $("#popup");
ocolorsul = $("#colors_ul");
}
function clearcolors(){
//清除提示内容
ocolorsul.empty();
opopdiv.removeclass("show");
}
function setcolors(the_colors){
//显示提示框,传入的参数即为匹配出来的结果组成的数组
clearcolors(); //每输入一个字母就先清除原先的提示,再继续
opopdiv.addclass("show");
for(var i=0;i<the_colors.length;i++)
//将匹配的提示结果逐一显示给用户
ocolorsul.append($("<li>"+the_colors[i]+"</li>"));
ocolorsul.find("li").click(function(){
oinputfield.val($(this).text());
clearcolors();
}).hover(
function(){$(this).addclass("mouseover");},
function(){$(this).removeclass("mouseover");}
);
}
function findcolors(){
initvars(); //初始化变量
if(oinputfield.val().length > 0){
//获取异步数据
$.get("14-10.aspx",{scolor:oinputfield.val()},
function(data){
var aresult = new array();
if(data.length > 0){
aresult = data.split(",");
setcolors(aresult); //显示服务器结果
}
else
clearcolors();
});
}
else
clearcolors(); //无输入时清除提示框(例如用户按del键)
}
</script>
</head>
<body>
<form method="post" name="myform1">
color: <input type="text" name="colors" id="colors" onkeyup="findcolors();" />
</form>
<div id="popup">
<ul id="colors_ul"></ul>
</div>
</body>
</html>
服务器端(14-10.aspx ):
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<%@ import namespace="system.data" %>
<%
response.cachecontrol = "no-cache";
response.addheader("pragma","no-cache");
string sinput = request["scolor"].trim();
if(sinput.length == 0)
return;
string sresult = "";
string[] acolors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass","bronze","brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue","feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod","golenrod","gostwhite","gray","green","greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray","snow","springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
for(int i=0;i<acolors.length;i++){
if(acolors[i].indexof(sinput) == 0)
sresult += acolors[i] + ",";
}
if(sresult.length>0) //如果有匹配项
sresult = sresult.substring(0,sresult.length-1); //去掉最后的“,”号
response.write(sresult);
%>
客户端:
复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<title>ajax实现自动提示的文本框</title>
<style>
<!--
body{
font-family:arial, helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* 用户输入框的样式 */
font-family:arial, helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* 提示框div块的样式 */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:arial, helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* 显示提示框的边框 */
border:1px solid #004a7e;
}
#popup.hide{
/* 隐藏提示框的边框 */
border:none;
}
/* 提示框的样式风格 */
ul{
list-style:none;
margin:0px; padding:0px;
}
li.mouseover{
background-color:#004a7e;
color:#ffffff;
}
li.mouseout{
background-color:#ffffff;
color:#004a7e;
}
-->
</style>
<script language="javascript">
var oinputfield; //考虑到很多函数中都要使用
var opopdiv; //因此采用全局变量的形式
var ocolorsul;
var xmlhttp;
function createxmlhttprequest(){
if(window.activexobject)
xmlhttp = new activexobject("microsoft.xmlhttp");
else if(window.xmlhttprequest)
xmlhttp = new xmlhttprequest();
}
function initvars(){
//初始化变量
oinputfield = document.forms["myform1"].colors;
opopdiv = document.getelementbyid("popup");
ocolorsul = document.getelementbyid("colors_ul");
}
function clearcolors(){
//清除提示内容
for(var i=ocolorsul.childnodes.length-1;i>=0;i--)
ocolorsul.removechild(ocolorsul.childnodes[i]);
opopdiv.classname = "hide";
}
function setcolors(the_colors){
//显示提示框,传入的参数即为匹配出来的结果组成的数组
clearcolors(); //每输入一个字母就先清除原先的提示,再继续
opopdiv.classname = "show";
var oli;
for(var i=0;i<the_colors.length;i++){
//将匹配的提示结果逐一显示给用户
oli = document.createelement("li");
ocolorsul.appendchild(oli);
oli.appendchild(document.createtextnode(the_colors[i]));
oli.onmouseover = function(){
this.classname = "mouseover"; //鼠标经过时高亮
}
oli.onmouseout = function(){
this.classname = "mouseout"; //离开时恢复原样
}
oli.onclick = function(){
//用户点击某个匹配项时,设置输入框为该项的值
oinputfield.value = this.firstchild.nodevalue;
clearcolors(); //同时清除提示框
}
}
}
function findcolors(){
initvars(); //初始化变量
if(oinputfield.value.length > 0){
createxmlhttprequest(); //将用户输入发送给服务器
var surl = "9-10.aspx?scolor=" + oinputfield.value + "×tamp=" + new date().gettime();
xmlhttp.open("get",surl,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readystate == 4 && xmlhttp.status == 200){
var aresult = new array();
if(xmlhttp.responsetext.length){
aresult = xmlhttp.responsetext.split(",");
setcolors(aresult); //显示服务器结果
}
else
clearcolors();
}
}
xmlhttp.send(null);
}
else
clearcolors(); //无输入时清除提示框(例如用户按del键)
}
</script>
</head>
<body>
<form method="post" name="myform1">
color: <input type="text" name="colors" id="colors" onkeyup="findcolors();" />
</form>
<div id="popup">
<ul id="colors_ul"></ul>
</div>
</body>
</html>
服务器端(9-10.aspx ):
复制代码 代码如下:
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<%@ import namespace="system.data" %>
<%
response.cachecontrol = "no-cache";
response.addheader("pragma","no-cache");
string sinput = request["scolor"].trim();
if(sinput.length == 0)
return;
string sresult = "";
string[] acolors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass","bronze","brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue","feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod","golenrod","gostwhite","gray","green","greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray","snow","springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
for(int i=0;i<acolors.length;i++){
if(acolors[i].indexof(sinput) == 0)
sresult += acolors[i] + ",";
}
if(sresult.length>0) //如果有匹配项
sresult = sresult.substring(0,sresult.length-1); //去掉最后的“,”号
response.write(sresult);
%>
示例二:使用jquery 实现。
客户端:
复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<title>jquery实现自动提示的文本框</title>
<style>
<!--
body{
font-family:arial, helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* 用户输入框的样式 */
font-family:arial, helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* 提示框div块的样式 */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:arial, helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* 显示提示框的边框 */
border:1px solid #004a7e;
}
/* 提示框的样式风格 */
ul{
list-style:none;
margin:0px; padding:0px;
color:#004a7e;
}
li.mouseover{
background-color:#004a7e;
color:#ffffff;
}
-->
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
var oinputfield; //考虑到很多函数中都要使用
var opopdiv; //因此采用全局变量的形式
var ocolorsul;
function initvars(){
//初始化变量
oinputfield = $("#colors");
opopdiv = $("#popup");
ocolorsul = $("#colors_ul");
}
function clearcolors(){
//清除提示内容
ocolorsul.empty();
opopdiv.removeclass("show");
}
function setcolors(the_colors){
//显示提示框,传入的参数即为匹配出来的结果组成的数组
clearcolors(); //每输入一个字母就先清除原先的提示,再继续
opopdiv.addclass("show");
for(var i=0;i<the_colors.length;i++)
//将匹配的提示结果逐一显示给用户
ocolorsul.append($("<li>"+the_colors[i]+"</li>"));
ocolorsul.find("li").click(function(){
oinputfield.val($(this).text());
clearcolors();
}).hover(
function(){$(this).addclass("mouseover");},
function(){$(this).removeclass("mouseover");}
);
}
function findcolors(){
initvars(); //初始化变量
if(oinputfield.val().length > 0){
//获取异步数据
$.get("14-10.aspx",{scolor:oinputfield.val()},
function(data){
var aresult = new array();
if(data.length > 0){
aresult = data.split(",");
setcolors(aresult); //显示服务器结果
}
else
clearcolors();
});
}
else
clearcolors(); //无输入时清除提示框(例如用户按del键)
}
</script>
</head>
<body>
<form method="post" name="myform1">
color: <input type="text" name="colors" id="colors" onkeyup="findcolors();" />
</form>
<div id="popup">
<ul id="colors_ul"></ul>
</div>
</body>
</html>
服务器端(14-10.aspx ):
复制代码 代码如下:
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<%@ import namespace="system.data" %>
<%
response.cachecontrol = "no-cache";
response.addheader("pragma","no-cache");
string sinput = request["scolor"].trim();
if(sinput.length == 0)
return;
string sresult = "";
string[] acolors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass","bronze","brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue","feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod","golenrod","gostwhite","gray","green","greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray","snow","springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
for(int i=0;i<acolors.length;i++){
if(acolors[i].indexof(sinput) == 0)
sresult += acolors[i] + ",";
}
if(sresult.length>0) //如果有匹配项
sresult = sresult.substring(0,sresult.length-1); //去掉最后的“,”号
response.write(sresult);
%>