go—web

作者: 哆啦在这A梦在哪 | 来源:发表于2018-07-05 15:13 被阅读19次

前端代码:


<head>
    <title></title>
</head>
<script>
    $(document).ready(function() {

        $("#fld货品编码").blur(function() { //失去光标触发事件
            alert($("#fld货品编码").val())
            jQuery.ajax({
                type: "get",
                url: location.href + "&bianma=" + encodeURIComponent($("#fld货品编码").val()),
                success: function(e) {
                    $("#fld货品名称").val(e)
                }
            })
        });
    });
</script>
<body>
    <div>
        <form action="http://localhost:8088/login" method="post">
            用户名:<input type="text" name="username"> 密码:
            <input type="password" name="password">
            <input type="submit" value="登录">
            <div>
                <input type="radio" name="ra" value="1">man
                <input type="radio" name="ra" value="2">woman
            </div>
            <div>
                <input type="checkbox" name="cb" value="1">1</checkbox>
                <input type="checkbox" name="cb" value="2">2</checkbox>
                <input type="checkbox" name="cb" value="3">3</checkbox>
                <input type="checkbox" name="cb" value="4">4</checkbox>
            </div>
            <div>
                <select name="st">
                        <option selected="selected" disabled="disabled"  style='display: none' value='' ></option>
                        <option value="foo">foo</option>
                        <option value="basket">basket</option>
                        <option value="table">table</option>
                        <option value="pine">pine</option>
                    </select>
            </div>
        </form>
    </div>

</body>

</html>


后台逻辑部分:

package main

import (
    "fmt"
    "html/template"
    "log"
    "net/http"
)

func main() {

    http.HandleFunc("/login", Login)
    http.HandleFunc("/aa", SockHandle) //记得使用除号隔开
    err := http.ListenAndServe(":8088", nil)
    if err != nil {
        log.Fatal(err)
    }
}

func Login(w http.ResponseWriter, r *http.Request) {
    log.Println("Method:", r.Method)
    Check(r)
    if r.Method == "Get" {
        t, _ := template.ParseFiles("login.html")
        fmt.Println(t.Execute(w, nil))
    } else {
        fmt.Println("name:", r.FormValue("username"))
        fmt.Println("password:", r.FormValue("password"))
        w.Write([]byte("success"))
    }
}

//将路由上的请求数据解析成一个[]map,采用键值对的方式可以访问
func SockHandle(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    fmt.Println("form:", r.Form)
    fmt.Println("name:", r.Form["name"])
    for k, v := range r.Form {
        fmt.Println("k:", k)
        fmt.Println("v:", v)
    }
    fmt.Fprint(w, "ok")
}

func Check(r *http.Request) {

    if r.FormValue("ra") == "" {
        fmt.Println("ra:", r.Form.Get("ra"))
        fmt.Println("radio can not null")
    }

    if r.FormValue("cb") == "" {
        fmt.Println("cb:", r.Form.Get("cb"))
        fmt.Println("checkbox can not null")
    }

    if r.Form.Get("st") == "" {
        fmt.Println("st:", r.Form.Get("st"))
        fmt.Println("select can not null")
    }

}

相关文章

网友评论

      本文标题:go—web

      本文链接:https://www.haomeiwen.com/subject/ewspuftx.html