jQuery autocomplate 自扩展插件、自动完成示例代码

2020-05-19 07:39:57易采站长站整理

怎么样,用法比较简单吧。当然后面你还可以加其他的配置,如:
代码片段

$(“:text”).autocomplete(“AutocomplataWordServlet”, {
width: “auto”,
highlightColor: “#3355FE”,
unhighlightColor: “#FFFFcc”,
css: {border: “2px solid red”},
dataType: “xml”,
paramName: “keyWord”,
delay: 300
});

这样也是可以的;
看看AutocomplataWordServlet的代码:

package com.hoo.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings(“serial”)
public class AutocomplataWordServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String word = request.getParameter(“word”);
request.setAttribute(“word”, word);
//System.out.println(word);
request.getRequestDispatcher(“word.jsp”).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

没什么可说的,就是获取客户端文本域的ajax请求的关键字,然后在jsp页面中进行单词过滤。不过你也可以在客户端用正则
或是在服务器端用正则过滤都是可以的。
下面看看word.jsp的内容:

<%@ page language=”java” contentType=”text/xml; charset=UTF-8″ %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
<words>
<c:if test=”${fn:startsWith(‘abstruct’, word)}”>
<word>abstruct</word>
</c:if>
<c:if test=”${fn:startsWith(‘anilazine’, word)}”>
<word>anilazine</word>
</c:if>
<c:if test=”${fn:startsWith(‘appeared’, word)}”>
<word>appeared</word>
</c:if>
<c:if test=”${fn:startsWith(‘autocytolysis’, word)}”>
<word>autocytolysis</word>
</c:if>
<c:if test=”${fn:startsWith(‘apple’, word)}”>
<word>apple</word>
</c:if>
<c:if test=”${fn:startsWith(‘boolean’, word)}”>
<word>boolean</word>
</c:if>
<c:if test=”${fn:startsWith(‘break’, word)}”>
<word>break</word>
</c:if>
<c:if test=”${fn:startsWith(‘bird’, word)}”>