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

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

GOLANG 中的復選框是否選中

GOLANG 中的復選框是否選中

Go
慕無忌1623718 2022-05-05 17:42:18
我正在嘗試使用 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)}
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

我自己解決了這個問題,action=方法是在選中和取消選中復選框時更改表單中的屬性,將其分成兩部分。以下是更新后的表格action=""。


{{range .Result}}

<form action="" name="{{.}}" method="POST">

    <label class="switch"> <input id="{{.}}" type="checkbox" class="box"  name="{{.}}" value="{{.}}"><span class="slider round"></span></label>

    <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){

        var ch = document.getElementById(result)

        var formname = form.name;

        if(ch.checked == true){

            document[formname].action="/up_dags/"+result

        }

        else if(ch.checked == false){

            document[formname].action="/p_dags/"+result

        }

      }

</script>

高朗代碼:


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)

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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