search
HomeWeb Front-endJS TutorialAjax implements intelligent prompt search function

Ajax implements intelligent prompt search function

May 23, 2018 pm 03:48 PM
ajaxhintsearch

This article mainly introduces the intelligent prompt search function of Ajax in detail, which has certain reference value. Interested friends can refer to it

1. Rendering:

2. Implementation process:

Ideas:

3. Part of the code:
html:

##

 <p id="searchbox">
  <p><input type="text" id="txtTitle" /></p>
  <p id="btnSelect"><a href="javascript:;">Google</a></p>
 </p>
 <p id="dtitles"></p>

css code:


* {
 padding:0px;
 margin:0px;
}

#searchbox {
 margin-top:10px;
 height:37px;
 width:550px;
}
#searchbox p {
 float:left;
} 
#txtTitle {
 height:35px;
 width:440px;
 line-height:35px;
 border:solid 1px #4791FF;
}
#btnSelect a{
 width:100px;
 height:37px;
 background:#167ED9;
 display:block;
 line-height:37px;
 color:#ffffff;
 text-align:center; 
}
a:link {
 text-decoration:none;
}
a:hover {
 cursor:pointer;
}
#dtitles {
 width:540px;
 height:90px;
 border:solid 1px #CCCCCC;
 display:none;
 font-size:12px;
}
.li1 {
 background:#F0F0F0;
}

js code:

$(function ()
{
 //1.页面加载之后,找到文本框的内容对它触发一个事件
 $("#txtTitle").keyup(function ()
 {
  //2.获取到文本框的内容,注意去空格
  var title = $.trim($("#txtTitle").val());
  //3.获取到输入的内容之后,就要通过ajax传给后台
  $.post("/Handler3.ashx", { "title": title }, function (data)
  {
   if (title == "") {
    $("#dtitles").hide();
   }
   else
   {
    //显示展示p,把它清空
    $("#dtitles").show().html("");
    if (data == "") {
     $("#dtitles").text("没有相关数据!");
    }
    else {
     $("#dtitles").append(data);
     //4.鼠标移上去之后,加一个背景
     $("li").hover(function ()
     {
      $(this).addClass("li1");
     }, function ()
     {
      $(this).removeClass("li1");
     });
    }
   }
  });
 });
});

ajax:

public void ProcessRequest(HttpContext context)
  {
   //1.提交过来之后,我们要接收
   string title=context.Request.Form["title"];
   //2.得到一个sql语句
   string strsql = string.Format("select top 5 title from RNews where Title like &#39;%{0}%&#39; ",title);
   //3.那得到sql怎么去做处理?
   DataTable dt = SqlHelper.ExecuteDataSetText(strsql,null).Tables[0];
   //4.我们最后要返回的是一个列表,要做字符串拼凑
   StringBuilder sb = new StringBuilder();
   //4.1判断得到的sql结果里面是否有数据
   if (dt.Rows.Count > 0)
   {
    //4.1.1
    sb.Append("<ul>");
    for (int i = 0; i < dt.Rows.Count; i++)
    {
     sb.Append(string.Format("<li>{0}</li>", dt.Rows[i][0].ToString()));
    }
    sb.Append("</ul>");
   }
   context.Response.Write(sb.ToString());
  }

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Open a new window address after the Ajax request is successful

Upload files through Ajax Use FormData for Ajax Request

jQuery Ajax method to upload files

The above is the detailed content of Ajax implements intelligent prompt search function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment