首页  >  文章  >  后端开发  >  Js实现带自动提示的文本框效果

Js实现带自动提示的文本框效果

WBOY
WBOY原创
2016-07-25 09:12:451243浏览

分享二个自动提示的文本框效果的js实现代码。

例一,直接编写AJAX 实现。 客户端:

  1. Ajax实现自动提示的文本框_bbs.it-home.org
  2. Color:
  3. 复制代码

    服务器端(9-10.aspx ):

    1. Response.CacheControl = "no-cache";
    2. Response.AddHeader("Pragma","no-cache");
    3. string sInput = Request["sColor"].Trim();
    4. if(sInput.Length == 0)
    5. return;
    6. string sResult = "";
    7. string[] aColors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass",
    8. "bronze","brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan",
    9. "darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid",
    10. "darkorenge","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink",
    11. "deepskyblue","dimgray","dodgerblue","feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod",
    12. "golenrod","gostwhite","gray","green","greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender",
    13. "lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray",
    14. "lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow",
    15. "lime","limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul",
    16. "mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream",
    17. "mistyrose","moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod",
    18. "palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red",
    19. "rosybrown","royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray",
    20. "snow","springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
    21. for(int i=0;iif(aColors[i].IndexOf(sInput) == 0)
    22. sResult += aColors[i] + ",";
    23. }
    24. if(sResult.Length>0) //如果有匹配项
    25. sResult = sResult.Substring(0,sResult.Length-1); //去掉最后的“,”号
    26. Response.Write(sResult);
    27. %>
    复制代码

    例二,使用jQuery 实现。 客户端:

    1. jQuery实现自动提示的文本框
    2. "));
    3. oColorsUl.find("li").click(function(){
    4. oInputField.val($(this).text());
    5. clearColors();
    6. }).hover(
    7. function(){$(this).addClass("mouseOver");},
    8. function(){$(this).removeClass("mouseOver");}
    9. );
    10. }
    11. function findColors(){
    12. initVars(); //初始化变量
    13. if(oInputField.val().length > 0){
    14. //获取异步数据
    15. $.get("14-10.aspx",{sColor:oInputField.val()},
    16. function(data){
    17. var aResult = new Array();
    18. if(data.length > 0){
    19. aResult = data.split(",");
    20. setColors(aResult); //显示服务器结果
    21. } bbs.it-home.org
    22. else
    23. clearColors();
    24. });
    25. }
    26. else
    27. clearColors(); //无输入时清除提示框(例如用户按del键)
    28. }
    29. Color:
    30. 复制代码

      服务器端(14-10.aspx ):

      1. Response.CacheControl = "no-cache";
      2. Response.AddHeader("Pragma","no-cache");
      3. string sInput = Request["sColor"].Trim();
      4. if(sInput.Length == 0)
      5. return;
      6. string sResult = "";
      7. string[] aColors = new string[]{"aliceblue","antiquewith","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brass","bronze",
      8. "brown","burlywood","cadetblue","chartreuse","chocolate","copper","coral","cornfloewrblue","cornsilk","cyan","darkblue","darkcyan",
      9. "darkgoldenrod","darkgray","darkgreen","darkhaki","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred","darksalmon",
      10. "darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue",
      11. "feldspar","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","gold","goldenrod","golenrod","gostwhite","gray","green",
      12. "greenyellow","honeydew","hotpink","indianred","inen","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon",
      13. "lightblue","lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray","lightgreen","lightpink","lightsalmon",
      14. "lightseagreen","lightskyblue","lightslateblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","magenta",
      15. "magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul","mediumseagreen",
      16. "mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose",
      17. "moccasin","navajowhite","navy","navyblue","oldlace","olivedrab","orange","orchid","orengered","palegodenrod","palegreen",
      18. "paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","quartz","red","rosybrown",
      19. "royalblue","saddlebrown","salmon","sandybrown","scarlet","seagreen","seashell","sienna","silver","skyblue","slategray","snow",
      20. "springgreen","steelblue","tan","thistle","tomato","turquoise","violet","violetred","wheat","whitesmoke","yellow","yellowgreen"};
      21. for(int i=0;iif(aColors[i].IndexOf(sInput) == 0)
      22. sResult += aColors[i] + ",";
      23. }
      24. if(sResult.Length>0) //如果有匹配项
      25. sResult = sResult.Substring(0,sResult.Length-1); //去掉最后的“,”号
      26. Response.Write(sResult);
      27. %>
      复制代码


      声明:
      本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn