繁体   English   中英

如何检测用户对网页表单的输入

[英]How to detect user input into a form on a webpage

我试图检测用户是否在由golang托管的网页上输入了某些内容,但是每次用户单击按钮并提交表单时,它只会重定向到另一个页面(我托管在localhost:8080上,并且重定向到localhost:8080 /文本)。 我很确定这是因为form动作设置为“ / text”,但是如果我删除,golang handlefunc永远不会运行。 这是我到目前为止的内容:

package main

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

func main()() {
  //runs server
  fs := http.FileServer(http.Dir("./"))
  http.Handle("/", fs)
  log.Println("Listening...")
  http.ListenAndServe(":8080", nil)
  //detects user input and calls function
  http.HandleFunc("/text", textGetter)
}

func textGetter (w http.ResponseWriter, r *http.Request) () {
  r.ParseForm()
  text := r.PostFormValue("text")
  fmt.Fprintf(w, "this: %s", text)
  fmt.Print(" was written in the text box")
}

和这个HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello</title>
    <link rel="stylesheet" href="./stylesheet.css">
  </head>
  <body>
    <h1>Write something bellow</h1>
    <form class="userText" action="/text" method="post">
      <input type="text" name="textbox" id="textBox" value="" placeholder="type something here">
      <input type="submit" value="text" name="button" id="button" onclick="thank(message.value)">
    </form>
    <script type="text/javascript" src="./script.js"></script>
  </body>
</html>

有人可以告诉我怎么了吗?

这是独立于Go或任何其他语言的前端任务。

您可以跟踪将onkeypress属性添加到感兴趣字段的用户输入。 您需要一些Ajax库。 例如,您可以使用jQuery

尝试类似的代码:

<input ... onkeypress="input()">
<script>
       function input(){
            $.ajax(url, parameters)
       }
 </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM