Use JavaScript to create a text box with automatic prompts. Friends who need it can refer to it. Example 1: Write AJAX implementation directly.
Client:
Copy the code code as follows:
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<head>
<title>Ajax realizes automatic prompt text box</title>
<style>
<!--
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* Style of user input box */
font-family:Arial, Helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* Style of prompt box div block */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:Arial, Helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* Display the border of the prompt box */
border:1px solid #004a7e;
}
#popup.hide{
/* Hide the border of the prompt box*/
border:none;
}
/* Style of prompt box*/
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; //Considering that it is used in many functions
var oPopDiv; //So it is in the form of a global variable
var oColorsUl;
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
else if(window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
}
function initVars(){
//Initialize variables
oInputField = document.forms[myForm1].colors;
oPopDiv = document.getElementById(popup);
oColorsUl = document.getElementById(colors_ul);
}
function clearColors(){
//Clear the prompt content
for(var i=oColorsUl.childNodes.length-1;i>=0;i--)
oColorsUl.removeChild(oColorsUl.childNodes[i]);
oPopDiv.className = hide;
}
function setColors(the_colors){
//Display a prompt box, and the parameter passed in is an array composed of the matched results.
clearColors(); //Each time you enter a letter, clear the original prompt before continuing.
oPopDiv.className = show;
var oLi;
for(var i=0;i<the_colors.length;i++){
//Display matching prompt results to the user one by one
oLi = document.createElement(li);
oColorsUl.appendChild(oLi);
oLi.appendChild(document.createTextNode(the_colors[i]));
oLi.onmouseover = function(){
this.className = mouseOver; //Highlight when the mouse passes over
}
oLi.onmouseout = function(){
this.className = mouseOut; //Restore to original state when leaving
}
oLi.onclick = function(){
//When the user clicks on a matching item, set the input box to the value of the item
oInputField.value = this.firstChild.nodeValue;
clearColors(); //Clear the prompt box at the same time
}
}
}
function findColors(){
initVars(); //Initialize variables
if(oInputField.value.length > 0){
createXMLHttpRequest(); //Send user input to the server
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); //Display server results
}
else
clearColors();
}
}
xmlHttp.send(null);
}
else
clearColors(); //Clear the prompt box when there is no input (for example, the user presses the del key)
}
</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>
Server side (9-10.aspx):
Copy the code code as follows:
<%@ 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,cornfloewrb lue,cornsilk,cyan,darkblue,darkcyan,darkgoldenrod,darkgray,darkgreen,darkhaki,darkmagenta,darkolivegreen,darkorchid,darkorenge,darkred,darksalmon,darkseagreen,darkslateblue,darkslate gray,darkturquoise,darkviolet,deeppink,deepskyblue,dimgray,dodgerblue,feldspar,firebrick,floralwhite,forestgreen,fuchsia,gainsboro,gold,goldenrod,golenrod,gostwhite,gray,green,greeny ellow,honeydew,hotpink,indianred,inen,ivory,khaki,lavender,lavenderblush,lawngreen,lemonchiffon,lightblue,lightcoral,lightcyan,lightgodenrod,lightgodenrodyellow,lightgray,lightgreen,l ightpink,lightsalmon,lightseagreen,lightskyblue,lightslateblue,lightslategray,lightsteelblue,lightyellow,lime,limegreen,magenta,magenta,maroom,maroon,mediumaquamarine,mediumblue,medi umorchid,mediumpurpul,mediumseagreen,mediumslateblue,mediumspringgreen,mediumturquoise,mediumvioletred,midnightblue,mintcream,mistyrose,moccasin,navajowhite,navy,navyblue,oldlace,oli vedrab,orange,orchid,orengered,palegodenrod,palegreen,paleturquoise,palevioletred,papayawhip,peachpuff,peru,pink,plum,powderblue,purple,quartz,red,rosybrown,royalblue,saddlebrown,sal mon,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) //If there is a match
sResult = sResult.Substring(0,sResult.Length-1); //Remove the last, number
Response.Write(sResult);
%>
Example 2: Implemented using jQuery.
Client:
Copy the code code as follows:
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<head>
<title>JQuery implements automatic prompt text box</title>
<style>
<!--
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px; padding:0px; margin:5px;
}
form{padding:0px; margin:0px;}
input{
/* Style of user input box */
font-family:Arial, Helvetica, sans-serif;
font-size:12px; border:1px solid #000000;
width:200px; padding:1px; margin:0px;
}
#popup{
/* Style of prompt box div block */
position:absolute; width:202px;
color:#004a7e; font-size:12px;
font-family:Arial, Helvetica, sans-serif;
left:41px; top:25px;
}
#popup.show{
/* Display the border of the prompt box */
border:1px solid #004a7e;
}
/* Style of prompt box*/
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; //Considering that it is used in many functions
var oPopDiv; //So it is in the form of a global variable
var oColorsUl;
function initVars(){
//Initialize variables
oInputField = $(#colors);
oPopDiv = $(#popup);
oColorsUl = $(#colors_ul);
}
function clearColors(){
//Clear the prompt content
oColorsUl.empty();
oPopDiv.removeClass(show);
}
function setColors(the_colors){
//Display a prompt box, and the parameter passed in is an array composed of the matched results.
clearColors(); //Each time you enter a letter, clear the original prompt before continuing.
oPopDiv.addClass(show);
for(var i=0;i<the_colors.length;i++)
//Display matching prompt results to the user one by one
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(); //Initialize variables
if(oInputField.val().length > 0){
//Get asynchronous data
$.get(14-10.aspx,{sColor:oInputField.val()},
function(data){
var aResult = new Array();
if(data.length > 0){
aResult = data.split(,);
setColors(aResult); //Display server results
}
else
clearColors();
});
}
else
clearColors(); //Clear the prompt box when there is no input (for example, the user presses the del key)
}
</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>
Server side (14-10.aspx):
Copy the code code as follows:
<%@ 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,cornfloewrb lue,cornsilk,cyan,darkblue,darkcyan,darkgoldenrod,darkgray,darkgreen,darkhaki,darkmagenta,darkolivegreen,darkorchid,darkorenge,darkred,darksalmon,darkseagreen,darkslateblue,darkslate gray,darkturquoise,darkviolet,deeppink,deepskyblue,dimgray,dodgerblue,feldspar,firebrick,floralwhite,forestgreen,fuchsia,gainsboro,gold,goldenrod,golenrod,gostwhite,gray,green,greeny ellow,honeydew,hotpink,indianred,inen,ivory,khaki,lavender,lavenderblush,lawngreen,lemonchiffon,lightblue,lightcoral,lightcyan,lightgodenrod,lightgodenrodyellow,lightgray,lightgreen,l ightpink,lightsalmon,lightseagreen,lightskyblue,lightslateblue,lightslategray,lightsteelblue,lightyellow,lime,limegreen,magenta,magenta,maroom,maroon,mediumaquamarine,mediumblue,medi umorchid,mediumpurpul,mediumseagreen,mediumslateblue,mediumspringgreen,mediumturquoise,mediumvioletred,midnightblue,mintcream,mistyrose,moccasin,navajowhite,navy,navyblue,oldlace,oli vedrab,orange,orchid,orengered,palegodenrod,palegreen,paleturquoise,palevioletred,papayawhip,peachpuff,peru,pink,plum,powderblue,purple,quartz,red,rosybrown,royalblue,saddlebrown,sal mon,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) //If there is a match
sResult = sResult.Substring(0,sResult.Length-1); //Remove the last, number
Response.Write(sResult);
%>