下拉菜单中的ajax

前端之家收集整理的这篇文章主要介绍了下拉菜单中的ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
  2. <script type="text/javascript">
  3.  
  4.  
  5. $(function(){
  6. $("#c").keyup(function(e){
  7. $.post("${pageContext.request.contextPath}/ajax/nickname","nickname="+$("#c").val(),function(a){
  8. //清除提示框中的旧信息
  9. $("tbody").empty();
  10. //获取输入框中的文本
  11. var txt=$("#c").val();
  12. if(txt.length>0){
  13. //从数据源中检索相关数据
  14. for(var i=0;i<a.length;i++){
  15. //相关
  16. //if(a[i].indexOf(txt)>=0){
  17. $("tbody").append("<tr>"+
  18. "<td>"+a[i].username+"</td>"+
  19. "</tr>"
  20. );
  21. //}
  22. }
  23. $("div").slideDown(200);
  24. }else{
  25. //隐藏提示
  26. $("div").hide();
  27. }
  28. //为提示框中的提示增加效果
  29. $("tbody").on("mouSEOver","tr",function(){
  30. $(this).css("background-color","#abcdef");
  31. });
  32. $("tbody").on("mouSEOut","#ffffff");
  33. });
  34. //为提示框中的提示增加单击事件
  35. $("tbody").on("click",function(){
  36. //将当前tr中的text设置给文本框
  37. var msg=$(this).text();
  38. $("#c").val(msg);
  39. //隐藏提示
  40. $("div").hide();
  41. });
  42. },"json"
  43. );
  44. });
  45. });
  46. </script>
  47.  
  48.  
  49. <body>
  50. <center>
  51. <input id="c" type="text" style="width:202px;margin-bottom:0"/>
  52. <div style="display:none;border:1px solid gray;width:200px">
  53. <table style="width:200px">
  54. <tbody>
  55.  
  56. </tbody>
  57. </table>
  58. </div>
  59. </center>
  60. </body>
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. //模糊查询
  70. public String getMessageUsername(){
  71. list = ms.queryMessageUsername(nickname);
  72. //序列化
  73. Object json = JSON.toJSON(list);
  74. HttpServletResponse response = ServletActionContext.getResponse();
  75. response.setCharacterEncoding("utf-8");
  76. try {
  77. response.getWriter().print(json);
  78. response.getWriter().flush();
  79. response.getWriter().close();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. return null;
  84. }

猜你在找的Ajax相关文章