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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

IT利刃出鞘   2021-12-11 我要评论
想了解HTML+jQuery实现简单的登录页面的相关内容吗,IT利刃出鞘在本文为您仔细讲解HTMLjQuery登录页面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:HTMLjQuery登录页面,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

2.输入用户名和密码

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

结果

示例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

2.输入用户名和密码

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

3.点击登录

4.点击确定

示例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

2.输入用户名和密码

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

3.点击登录

4.点击确定

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们