[英]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.