Go – 如何加载一个新的HTML表单

huangapple go评论157阅读模式
英文:

Go - How to load a new html form

问题

在Go程序中成功处理了来自HTML表单的jQuery Ajax Post之后,我该如何加载一个新的表单?我首先尝试将表单作为响应发送,并且JavaScript将其显示出来,但它没有清除旧的(现有的)表单。然后,我尝试在HTML的JavaScript中使用"window.location = 'localhost:8088/MaintForm/'"来设置URL。这导致浏览器发出警告,但没有加载表单,也没有改变URL。我希望能够了解两种方法 - 通过充当服务器的Go程序和通过JavaScript。如果我手动更改URL,表单就可以正常加载。我想要做的是在JavaScript(jQuery Ajax)中接收响应,然后如果响应是积极的,则请求新的表单。我希望在不更改URL的情况下完成这个操作。正如我上面所说,这部分起作用了。

英文:

After processing a jQuery Ajax Post from an HTML form successfully within a Go program, how do I load a new form? I first tried sending the form as the response and the Javascript displayed it, but it did not clear the old (existing) form. I then tried within the HTML Javascript to set the URL using "window.location = 'localhost:8088/MaintForm/'". That resulted in a warning from the browser but did not load the form and did not change the URL. I would like to ideally know both methods - via the Go program acting as a server, and via Javascript. If I manually change the URL, the form loads OK. What I am trying to do is receive a response in Javascript (jQuery Ajax), and then request the new form if the response is positive. I would prefer to do this without changing the URL. As I said above, this partially worked.

答案1

得分: 1

你需要将原始表单放在一个标签内,例如一个div,并使用你的JQuery代码将该标签的内容替换为新的表单。这样你就不会改变URL。

这更多是一个关于javascript/JQuery的问题,而不是go特定的问题。

英文:

You would have to put your original form inside a tag, for example a div, and use your JQuery code to replace the contents of that tag with the new form. This way you are not changing the URL.

This is more of a javascript/JQuery question than a go-specific one.

答案2

得分: 0

在JavaScript中:

location.href = '/MaintForm/';

在Go语言中,你可以使用http.Redirect函数,像这样:

http.Redirect(w, r, "/MaintForm/", http.StatusFound)
英文:

In javascript:

location.href = '/MaintForm/';

In golang, you can use the http.Redirect function, like this:

http.Redirect(w, r, "/MaintForm/", http.StatusFound)

答案3

得分: 0

请注意:这个问题似乎已经通过在Javascript中执行"document.write(data);"来解决。"Data"包含了新的HTML内容。

英文:

Please note: this appears to be solved by : I just need to do an "document.write(data);" in Javascript. "Data" contains the new HTML.

huangapple
  • 本文由 发表于 2011年5月15日 01:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/6003701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定