HTML+jQuery如何实现简单的登录页面

这篇文章主要介绍“HTML+jQuery如何实现简单的登录页面”,在日常操作中,相信很多人在HTML+jQuery如何实现简单的登录页面问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”HTML+jQuery如何实现简单的登录页面”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

简介

本文用示例展示简单的登录页面的写法。

会包括如下几种方案:纯HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。

公共代码(后端接口)

用SpringBoot写一个最简单的登录接口。

Controller

package com.example.controller;
 
import com.example.entity.LoginVO;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
//跨域
@CrossOrigin
//Rest风格:返回JSON
@RestController
public class LoginController {
    @PostMapping("login")
    public LoginVO login() {
        //省略对用户名和密码的判断
        LoginVO loginVO = new LoginVO();
        loginVO.setSuccess(true);
        loginVO.setData("This is data");
        return loginVO;
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo_SpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_SpringBoot</name>
    <description>Demo project for Spring Boot</description>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

示例1:最简(纯HTML)

代码

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页</title>
</head>
 
<body>
 
<form action="http://localhost:8080/login" method="post">
    <label for="username">用户名:</label>
    <input type="text" name="username" id="username">
 
    <label for="password">密码:</label>
    <input type="password" name="password" id="password">
 
    <!--下边这样写也可以
    <label for="username">
        用户名:<input type="text" name="username" id="username">
    </label>
    <label for="password">
        密码:<input type="password" name="password" id="password">
    </label>-->
 
    <button type="submit">登录</button>
</form>
 
</body>
</html>

测试

1.访问login.html

HTML+jQuery如何实现简单的登录页面  html 第1张

2.输入用户名和密码

用户名:输入abc;密码:输入 1234

结果

HTML+jQuery如何实现简单的登录页面  html 第2张

示例2:HTML+jQuery(form data)

代码

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页</title>
    <script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
</head>
 
<body>
 
<form id="login-form">
    <label for="username">用户名:</label>
    <input type="text" name="username" id="username">
 
    <label for="password">密码:</label>
    <input type="password" name="password" id="password">
</form>
 
<div id="error-message"></div>
<button type="submit" onclick="loginViaFormData()">登录</button>
 
<script>
    function loginViaFormData() {
        $.ajax(
            {
                type: "post",
                url: "http://localhost:8080/login",
                data: $("#login-form").serialize(), // 序列化form表单里面的数据传到后台
                //dataType: "json", // 指定后台传过来的数据是json格式
                success: function (result) {
                    if (!result.success) {
                        $("#errormessage").text("用户名或密码错误");
                    } else if (result.success) {
                        alert("登录成功");
                        // 跳到index.html页面
                        window.location.href="index.html" rel="external nofollow"  rel="external nofollow" ;
                    }
                }
            }
        )
    }
</script>
 
</body>
</html>

index.html

<!doctype html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>This is title</title>
</head>
 
<body>
 
<div class="container">
    登录成功后的页面
</div>
 
<script>
 
</script>
</body>
</html>

测试

1.访问login.html

HTML+jQuery如何实现简单的登录页面  html 第3张

2.输入用户名和密码

用户名:输入abc;密码:输入 1234

HTML+jQuery如何实现简单的登录页面  html 第4张

3.点击登录

HTML+jQuery如何实现简单的登录页面  html 第5张

4.点击确定

HTML+jQuery如何实现简单的登录页面  html 第6张

示例3:HTML+jQuery(json)

代码

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页</title>
    <script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
</head>
 
<body>
 
<form id="login-form">
    <label for="username">用户名:</label>
    <input type="text" name="username" id="username">
 
    <label for="password">密码:</label>
    <input type="password" name="password" id="password">
</form>
 
<div id="error-message"></div>
<button type="submit" onclick="loginViaJson()">登录</button>
 
<script>
    function loginViaJson() {
        $.post("http://localhost:8080/login",
            //发送给后端的数据
            {
                "userName": $(".username").val(),
                "password": $(".password").val()
            },
            //回调函数
            function (result) {
                if (!result.success) {
                    $("#errormessage").text("用户名或密码错误");
                } else if (result.success) {
                    alert("登录成功");
                    // 跳到index.html页面
                    window.location.href="index.html" rel="external nofollow"  rel="external nofollow" ;
                }
            }
        )
    }
</script>
 
</body>
</html>

index.html

<!doctype html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>This is title</title>
</head>
 
<body>
 
<div class="container">
    登录成功后的页面
</div>
 
<script>
 
</script>
</body>
</html>

测试

测试结果和前边“示例2:HTML+jQuery(form data)”一样

1.访问login.html

HTML+jQuery如何实现简单的登录页面  html 第7张

2.输入用户名和密码

用户名:输入abc;密码:输入 1234

HTML+jQuery如何实现简单的登录页面  html 第8张

3.点击登录

HTML+jQuery如何实现简单的登录页面  html 第9张

4.点击确定

HTML+jQuery如何实现简单的登录页面  html 第10张

到此,关于“HTML+jQuery如何实现简单的登录页面”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注蜗牛博客网站,小编会继续努力为大家带来更多实用的文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接