猿问

GOLANG 中的复选框是否选中

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


PS: .Result是一个字符串列表。


{{ range .Result}}

<form action="/p_up_dags/{{.}}" method="POST">

     <br>  <input id={{.}} type="checkbox" name="{{.}}" value="{{.}}" >  {{.}}

    </form>

{{end}}

戈朗代码:


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)

}


慕无忌1623718
浏览 128回答 1
1回答

慕慕森

我自己解决了这个问题,action=方法是在选中和取消选中复选框时更改表单中的属性,将其分成两部分。以下是更新后的表格action=""。{{range .Result}}<form action="" name="{{.}}" method="POST">&nbsp; &nbsp; <label class="switch"> <input id="{{.}}" type="checkbox" class="box"&nbsp; name="{{.}}" value="{{.}}"><span class="slider round"></span></label>&nbsp; &nbsp; <button onclick="myFunction(this.form,'{{.}}')">Pause/Unpause</button></form>{{end}}JS 代码: 在这里,我们检查复选框是选中还是未选中,然后分别更改表单操作。if checked then action="blah/blah" else action="foo/foo"<script>function myFunction(form,result){&nbsp; &nbsp; &nbsp; &nbsp; var ch = document.getElementById(result)&nbsp; &nbsp; &nbsp; &nbsp; var formname = form.name;&nbsp; &nbsp; &nbsp; &nbsp; if(ch.checked == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document[formname].action="/up_dags/"+result&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ch.checked == false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document[formname].action="/p_dags/"+result&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }</script>高朗代码:func p_dags(w http.ResponseWriter, r *http.Request){&nbsp; &nbsp;d_name := mux.Vars(r)["name"]&nbsp; &nbsp;_, err := http.Get(foo/foo/d_name)}func up_dags(w http.ResponseWriter, r *http.Request){&nbsp; &nbsp;d_name := mux.Vars(r)["name"]&nbsp; &nbsp;_, err := http.Get(blah/blah/d_name)}func main(){&nbsp; &nbsp; router := mux.NewRouter()&nbsp; &nbsp; router.HandleFunc("/p_dags/{name}",p_dags)&nbsp; &nbsp; router.HandleFunc("/up_dags/{name}",up_dags)}
随时随地看视频慕课网APP

相关分类

Go
我要回答