登录
首页 >  Golang >  Go问答

检查 Golang 中复选框是否勾选

来源:stackoverflow

时间:2024-03-24 12:39:42 411浏览 收藏

在 Go 应用程序中,可以使用表单操作来检测复选框是否被选中。在 HTML 中,通过 action 属性指定要调用的 API 端点。本文介绍了一种方法,通过根据复选框的状态动态更改表单操作来实现复选框选中或取消选中的检测。通过 JavaScript 函数,可以检查复选框的状态,并相应地更新表单操作,从而调用不同的 API 端点。

问题内容

我正在尝试使用 golang 构建一个简单的 web 应用程序。目标是,当用户选中复选框时,它必须调用一个 api 端点,而当用户取消选中复选框时,它必须调用另一个 api 端点。下面是我在 html 中使用 action="p_up_dags/{{.}}" 定义的表单。

ps: .result 是一个字符串列表。

{{ range .result}}

{{.}}
{{end}}

golang代码:

func p_up_dags(w http.ResponseWriter, r *http.Request){
     d_name = mux.Vars(r)["name"]
    //do something to check if the checkbox is checked or not
    //Something like this
    if d_name is checked
       {
       //http.Get("blah/blah")
       }
    else
      {
       //http.Get("foo/foo")
      }
}

func main(){
     router := mux.NewRouter().StrictSlash(true)
     router.HandleFunc("/p_up_dags/{name}",p_up_dags)
}

解决方案


我自己解决了这个问题,方法是在选中和取消选中复选框时更改表单中的 action= 属性,将其分成两部分。 下面是更新后的表单,其中包含 action=""

{{range .result}}
{{end}}

js 代码: 在这里,我们检查复选框是否已选中或未选中,然后分别更改表单操作。 if 检查然后 action="blah/blah" else action="foo/foo"

golang代码:

func p_dags(w http.ResponseWriter, r *http.Request){
   d_name := mux.Vars(r)["name"]
   _, err := http.Get(foo/foo/d_name)
}

func up_dags(w http.ResponseWriter, r *http.Request){
   d_name := mux.Vars(r)["name"]
   _, err := http.Get(blah/blah/d_name)
}

func main(){
    router := mux.NewRouter()
    router.HandleFunc("/p_dags/{name}",p_dags)
    router.HandleFunc("/up_dags/{name}",up_dags)
}

理论要掌握,实操不能落!以上关于《检查 Golang 中复选框是否勾选》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>