title: gin判断点击submit提交按钮的值
date: 2023-02-28 20:59:53.0
updated: 2023-05-05 17:53:07.0
url: https://liumou.site/doc/620
categories:

  • HTML
  • 前端
  • 后端
  • Gin
    tags:
  • Go

网页效果

网页效果

处理效果

点击安装

image-1683280352968

点击检查

image-1683280363513

处理日志

image-1683280378074

源码

Gin

通过获取name的值,从而获取value

func (api *User) install() {
    // http://127.0.0.1:8082/api/install?password=liumou
    api.Route.POST("/req", func(c *gin.Context) {
        api.Password = c.DefaultPostForm("password", "123") // 获取密码的值,如果为空则设置默认值: 123
        install := c.PostForm("install")
        check := c.PostForm("check")
        search := c.PostForm("search")
        var submit string
        if install == "安装" {
            submit = "install"
        }
        if check == "检查" {
            submit = "check"
        }
        if search == "查询" {
            submit = "search"
        }
        fmt.Println("点击了", submit)
        c.String(http.StatusOK, "操作: %s", submit)
    })
}

Html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{{.title}}</title>
    <link rel="stylesheet" href="/static/css/install.css" type="text/css" />
</head>
<body>

<form action="/req" method="post">
    <p>
        <label>
        <input type="password" name="password" placeholder="设置主机密码,默认: 123" size="30">
        </label>
    </p>
    <p>
        <input type="submit" name="install" value="安装">
        <input type="submit" name="check" value="检查">
        <input type="submit" name="search" value="查询">
    </p>
</form>


<p style="text-align: right">&#8212; By 坐公交也用券</p>

</body>
</html>