简体   繁体   English

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

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

I am trying to detect if the user enters something on a webpage hosted by golang but every time the user clicks the button and submits the form it just redirects to a different page (I am hosting on localhost:8080 and it redirects to localhost:8080/text). 我试图检测用户是否在由golang托管的网页上输入了某些内容,但是每次用户单击按钮并提交表单时,它只会重定向到另一个页面(我托管在localhost:8080上,并且重定向到localhost:8080 /文本)。 I am pretty sure this is because the form action is set to "/text" but if I remove that the golang handlefunc is never run. 我很确定这是因为form动作设置为“ / text”,但是如果我删除,golang handlefunc永远不会运行。 Here is what I have so far: 这是我到目前为止的内容:

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")
}

and this html: 和这个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>

Can someone tell me what's wrong? 有人可以告诉我怎么了吗?

It's a front end task independent of Go or any other language. 这是独立于Go或任何其他语言的前端任务。

You may track user input adding onkeypress attribute to the interesting field. 您可以跟踪将onkeypress属性添加到感兴趣字段的用户输入。 You need some Ajax library. 您需要一些Ajax库。 For example you may take jQuery . 例如,您可以使用jQuery

Try code similar to: 尝试类似的代码:

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

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

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