JS+Ajax實(shí)現(xiàn)百度智能搜索框
首先瀏覽實(shí)現(xiàn)后的結(jié)果,輸入一個(gè)a之后會(huì)出現(xiàn)包含a的下拉列表,當(dāng)我們點(diǎn)擊某一個(gè)的時(shí)候,搜索框中就會(huì)出現(xiàn)點(diǎn)擊的值。實(shí)現(xiàn)所需要的主要是ajax+js。

前端search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
<script src="js/jquery.min.js"></script>
<style type="text/css">
*{
margin: 0 auto;
padding: 0;
}
li{
margin:0;
height: 20px;
width: 200px;
list-style: none;
}
/* #c li:HOVER{
background-color: #F9F9F9;
} */
.onmouse{
background-color: #F9F9F9;
}
.outmouse{
background-color:white;
}
#contain{
width: 50%;
}
</style>
<!-- jquery -->
<script type="text/javascript">
var xmlHttp;
function getMoreContents() {
var content=document.getElementById("keyword");
if(content.value==""){
ClearContent();
return;//如果不設(shè)置,傳到后臺(tái)的是空值,所有的值都會(huì)被輸出
}
xmlHttp=creatXMLHttp();
//alert(xmlHttp);
//要給服務(wù)器發(fā)送數(shù)據(jù)
var url="searchServlet?keyword="+escape(content.value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
//獲取XMLHttp對(duì)象
function creatXMLHttp() {
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(!xmlHttp){
xmlHttp=new ActiveXOject("Msxml2.XMLHTTP");
}
}
return xmlHttp;
} //獲取XMLHttp對(duì)象
function callback() {
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var result=xmlHttp.responseText;
//解析數(shù)據(jù)
var json=eval("("+result+")");
//動(dòng)態(tài)顯示數(shù)據(jù),線束數(shù)據(jù)在輸入框?qū)Φ南旅?
setContent(json);
}
}
//設(shè)置關(guān)聯(lián)數(shù)據(jù)的展示
function setContent(contents) {
ClearContent();
var size=contents.length;
for(var i=0;i<size;i++)
{
var nextNode=contents[i];//json格式的第i個(gè)數(shù)據(jù)
var li =document.createElement("li");
li.onmouseover=function(){
this.className="onmouse";
document.getElementById("keyword").value=this.innerHTML;
}
li.onmouseout=function(){
this.className="outmouse";
}
var text=document.createTextNode(nextNode);
li.appendChild(text);
document.getElementById("c").appendChild(li);
}
}
//清空數(shù)據(jù)
function ClearContent() {
document.getElementById("c").innerHTML="";
}
//當(dāng)控件失去焦點(diǎn)時(shí),清空數(shù)據(jù)
function outFouce() {
ClearContent();
}
//獲得焦點(diǎn)時(shí),
</script>
</head>
<body>
<div id="contain">
<div style="height: 20px;">
<input type="text" id="keyword" style="size:50px;" onkeyup="getMoreContents()" onblur="outFouce()" onfocus="getMoreContents()">
<input type="button" id="bu" value="百度一下" style="">
</div>
<div id="popDiv">
<ul id="c">
<li></li>
</ul>
</div>
</div>
</body>
</html>
后臺(tái)searchServlet.Java
package search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class searchServlet
*/
@WebServlet("/searchServlet")
public class searchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static List<String> datas=new ArrayList<String>();
static {//假數(shù)據(jù),模擬數(shù)據(jù)庫(kù)讀取
datas.add("ajax");
datas.add("bjax");
datas.add("ajaxfd");
datas.add("bfvd");
datas.add("dafdx");
datas.add("fdax");
datas.add("ahg");
datas.add("ddx");
}
/**
* @see HttpServlet#HttpServlet()
*/
public searchServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String keyword=request.getParameter("keyword");
//System.out.println(keyword);
List<String> listdata= getData(keyword);
// 返回json,以流的形式寫到前臺(tái)
response.getWriter().write(JSONArray.fromObject(listdata).toString());
}
//獲取假數(shù)據(jù)中符合條件的值
public List<String> getData(String keyword)
{
List<String> list=new ArrayList<String>();
for(String data:datas)
{
if(data.contains(keyword)){
list.add(data);
}
}
return list;
}
}
xml配置
<span style="font-size:18px;"><strong><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>searchServlet</servlet-name> <servlet-class>search.searchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>searchServlet</servlet-name> <url-pattern>/search/searchServlet</url-pattern> </servlet-mapping> <display-name>DropMeum</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app></strong></span>
目錄結(jié)構(gòu)

總結(jié)
以上所述是小編給大家介紹的JS+Ajax實(shí)現(xiàn)百度智能搜索框,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- js實(shí)現(xiàn)搜索框關(guān)鍵字智能匹配代碼
- 基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
- JavaScript實(shí)現(xiàn)搜索框的自動(dòng)完成功能(一)
- JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
- JavaScript實(shí)現(xiàn)百度搜索框效果
- 自動(dòng)完成的搜索框javascript實(shí)現(xiàn)
- JS實(shí)現(xiàn)京東首頁(yè)之頁(yè)面頂部、Logo和搜索框功能
- JS實(shí)現(xiàn)微信彈出搜索框 多條件查詢功能
- javascript搜索框效果實(shí)現(xiàn)方法
- JavaScript仿京東搜索框?qū)嵗?/a>
相關(guān)文章
window.open()實(shí)現(xiàn)post傳遞參數(shù)
本文主要向大家介紹了如何使用window.open()實(shí)現(xiàn)post傳遞參數(shù)的方法,思路是參考的一位網(wǎng)友的,然后根據(jù)自己的項(xiàng)目需求做了些調(diào)整,這里同樣分享給大家,希望對(duì)大家能夠有所幫助。2015-03-03
js模仿windows桌面圖標(biāo)排列算法具體實(shí)現(xiàn)(附圖)
需要引入Jquery,如果需要全部功能,請(qǐng)引入jquery-ui和jquery-ui.css,具體實(shí)現(xiàn)步驟如下,感興趣的朋友可以參考下哈2013-06-06
關(guān)于Webpack dev server熱加載失敗的解決方法
下面小編就為大家分享一篇關(guān)于Webpack dev server熱加載失敗的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-02-02

