亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Echo 框架中綁定復選框?

如何在 Echo 框架中綁定復選框?

Go
阿晨1998 2022-06-21 10:47:39
我有一個簡單的表單,我想在發布請求上綁定它。這是表格:<form method="post" action="/post">    <input type="text" name="name" placeholder="name"><br>    <input type="checkbox" name="agree"><br>    <button type="submit">submit</button></form>我正在嘗試將其綁定在這樣的結構中:type PostForm struct {    Name  string    Agree bool}這是整個代碼:package mainimport (    "github.com/labstack/echo/v4"    "html/template"    "io"    "log"    "net/http")type Template struct {    templates *template.Template}func (t *Template) Render(w io.Writer, name string, data interface{}, _ echo.Context) error {    return t.templates.ExecuteTemplate(w, name, data)}type PostForm struct {    Name  string    Agree bool}func main() {    e := echo.New()    e.Debug = true    e.Renderer = &Template{        templates: template.Must(template.ParseGlob("./templates/*.gohtml")),    }    e.GET("/", func(c echo.Context) error {        return c.Render(http.StatusOK, "index.gohtml", nil)    })    e.POST("/post", func(c echo.Context) error {        var form PostForm        err := c.Bind(&form)        if err != nil {            return c.String(http.StatusInternalServerError, err.Error())        }        return c.JSON(http.StatusOK, form)    })    log.Fatalln(e.Start(":3000"))}當我使用未選中的同意字段發布請求時,它工作正常:{  "Name": "sdfgsdfg",  "Agree": false}但是當我發送帶有選中復選框的帖子時,出現錯誤:code=400, message=strconv.ParseBool: parsing "on": invalid syntax, internal=strconv.ParseBool: parsing "on": invalid syntax我做錯了什么?這是 github 上的所有代碼的倉庫:https ://github.com/max-block/q__echo_bind_checkbox
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

<html>

<head></head>

<body>

<form method="post" action="/post">

    <input type="text" name="name" placeholder="name"><br>

    // set the value as "true"

    <input type="checkbox" name="agree" value="true"><br>

    <button type="submit">submit</button>

</form>

</body>

</html>

checkbox的默認值為“on”,與go的“true”不同。


查看完整回答
反對 回復 2022-06-21
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號