Ajax小案例:驗證表單中使用者名稱是否可用__Ajax

來源:互聯網
上載者:User
需求:輸入使用者名稱判斷是否存在此使用者 建立表單index.jsp
<%--  Created by IntelliJ IDEA.  User: pc  Date: 17-4-21  Time: 下午5:15  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>註冊資訊</title>    <script type='text/javascript' src='../js/jquery.min.js' charset='utf-8'></script>    <style>        .top {            margin-left: 800px;            margin-top: 200px;        }    </style>    <script type="text/javascript">        $(function () {            $(":input[name='username']").change(function () {                var name = $(this).val();                name = $.trim(name);//取出前後空格                if (name != "") {                    var url = "/Login_AServlet";                    var args = {"username": name, "time": new Date()};                    $.get(url, args, function (data) {                        $(".message").html(data)                    });                }            });        });    </script></head><body><div class="top">    <h1>資訊註冊</h1>    <form action="#" method="post">        使用者名稱:<input type="text" name="username"><span class="message"> </span><br>        郵 箱:<input type="text" name="emile"><br>        性 別:<input type="radio" name="sex" value="男生" checked="checked">男生        <input type="radio" name="sex" value="女生">女生<br>        <input type="submit" value="登 錄">    </form></div></body></html>
Login_AServlet.java
<%--  Created by IntelliJ IDEA.  User: pc  Date: 17-4-21  Time: 下午12:10  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <script type='text/javascript' src='./js/jquery.min.js' charset='utf-8'></script>    <title>Title</title>    <script type="text/javascript">        $(function () {            $("a").click(function () {                var url = this.href;                var time = {"time": new Date()}                $.post(url, time, function (data) {                    var name = $(data).find("name").text();                    var age = $(data).find("age").text();                    var email = $(data).find("email").text();                    $(".html_").html("");                    $(".html_").append("<h2>姓名:" + name + "性別:" + age + "郵箱:" + email);                });                return false;            });        });    </script>    <title>Title</title></head><body><h1>學生資訊:</h1><ul>    <li><a href="./xml_ajax/huan.xml">馬小歡</a></li>    <li><a href="./xml_ajax/yun.xml">趙雲</a></li>    <li><a href="./xml_ajax/ming.xml">小明</a></li></ul><h1 class="html_"></h1></body></html>

文章文集:JavaEE–學習筆記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.