欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

HTML+jQuery實(shí)現(xiàn)簡(jiǎn)單的登錄頁(yè)面

 更新時(shí)間:2021年12月11日 08:52:15   作者:IT利刃出鞘  
這篇文章主要介紹了用三種方法實(shí)現(xiàn)簡(jiǎn)單的登錄頁(yè)面的制作:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。感興趣的同學(xué)可以跟隨小編一起學(xué)習(xí)一下

簡(jiǎn)介

本文用示例展示簡(jiǎn)單的登錄頁(yè)面的寫法。

會(huì)包括如下幾種方案:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。

公共代碼(后端接口)

用SpringBoot寫一個(gè)最簡(jiǎn)單的登錄接口。

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風(fēng)格:返回JSON
@RestController
public class LoginController {
    @PostMapping("login")
    public LoginVO login() {
        //省略對(duì)用戶名和密碼的判斷
        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:最簡(jiǎn)(純HTML)

代碼

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登錄頁(yè)</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>

測(cè)試

1.訪問(wèn)login.html

2.輸入用戶名和密碼

用戶名:輸入abc;密碼:輸入 1234

結(jié)果

示例2:HTML+jQuery(form data)

代碼

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登錄頁(yè)</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表單里面的數(shù)據(jù)傳到后臺(tái)
                //dataType: "json", // 指定后臺(tái)傳過(guò)來(lái)的數(shù)據(jù)是json格式
                success: function (result) {
                    if (!result.success) {
                        $("#errormessage").text("用戶名或密碼錯(cuò)誤");
                    } else if (result.success) {
                        alert("登錄成功");
                        // 跳到index.html頁(yè)面
                        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">
    登錄成功后的頁(yè)面
</div>
 
<script>
 
</script>
</body>
</html>

測(cè)試

1.訪問(wèn)login.html

2.輸入用戶名和密碼

用戶名:輸入abc;密碼:輸入 1234

3.點(diǎn)擊登錄

4.點(diǎn)擊確定

示例3:HTML+jQuery(json)

代碼

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登錄頁(yè)</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",
            //發(fā)送給后端的數(shù)據(jù)
            {
                "userName": $(".username").val(),
                "password": $(".password").val()
            },
            //回調(diào)函數(shù)
            function (result) {
                if (!result.success) {
                    $("#errormessage").text("用戶名或密碼錯(cuò)誤");
                } else if (result.success) {
                    alert("登錄成功");
                    // 跳到index.html頁(yè)面
                    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">
    登錄成功后的頁(yè)面
</div>
 
<script>
 
</script>
</body>
</html>

測(cè)試

測(cè)試結(jié)果和前邊“示例2:HTML+jQuery(form data)”一樣

1.訪問(wèn)login.html

2.輸入用戶名和密碼

用戶名:輸入abc;密碼:輸入 1234

3.點(diǎn)擊登錄

4.點(diǎn)擊確定

到此這篇關(guān)于HTML+jQuery實(shí)現(xiàn)簡(jiǎn)單的登錄頁(yè)面的文章就介紹到這了,更多相關(guān)HTML jQuery 登錄頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論