JQuery中使用a-jax局部书刷新验证表单

来源:互联网 发布:手机淘宝怎么看优惠券 编辑:程序博客网 时间:2024/04/30 11:16
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        /*$(function(){
            $("#ac").blur(function(){
                var userNo = $(this).val();
                $.post("test","userNo="+userNo,function(response){
                    $("#shit").text(response);
                });
            });
        });*/    
        $(function(){
            $("#ac").blur(function(){
                var userNo = $(this).val();
                $.get("test?userNo="+userNo,null,function(response){
                    $("#shit").text(response);
                });
            });
        });        
    </script>
  </head>
 
  <body>
    <form action = "#" method = post>
        账号:<input name="account" id="ac">
        <div style="display:inline" id="shit"></div>
        <br/>
        <input type="submit" />
    </form>
  </body>

</html>



==============================servlet:=======================================

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class test extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public test() {
        super();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String temp = request.getParameter("userNo");
        if(temp.equals("zhangsan")){
            out.println("账号可用");
        }else{
            out.println("账号不可用");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String temp = request.getParameter("userNo");
        if(temp.equals("zhangsan")){
            out.println("账号可用");
        }else{
            out.println("账号不可用");
        }
    }

}


=====================================

jQuery.get(url, [data], [callback], [type])

jQuery.post(url, [data], [callback], [type])

url:发送请求地址。

data:待发送 Key/value 参数。

callback:发送成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。


0 0