Golang JavaScript UI问题

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

golang javascript ui issue

问题

以下是翻译好的内容:

以下的HTML代码可以独立工作:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<script type="text/javascript" src="./jquery-1.7.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){

				$('.del').live('click',function(){
					$(this).parent().parent().remove();
				});

				$('.add').live('click',function(){
					$(this).val('Delete');
					$(this).attr('class','del');
					var appendTxt = "<tr><td><input type='text' name='name[]' /></td> <td><input type='text' name='value[]' /></td> <td><input type='checkbox' name='regex[]' /></td><td><input type='button' class='add' value='Add More' /></td></tr>";
					$("tr:last").after(appendTxt);
				});
			});
		</script>
		<style>
			table {
			  table-layout: fixed;
			  width:1200px;
			}
			input{
			  width:285px;
			}
		</style>
	 </head>

    <body>
		<form method="post" action="/silencealert">
		<table id="options-table">
			<tr>
				<td>Name</td>
				<td>Value</td>
				<td>Regex</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td><input type="text"   name="name[]" /></td>
				<td><input type="text"   name="value[]" /></td>
				<td><input type="checkbox" 	name="regex[]" /></td>
				<td><input type="button" class='del' value='Delete' /></td>
			</tr>
			<tr>
				<td><input type="text"   name="name[]" /></td>
				<td><input type="text"   name="value[]" /></td>
				<td><input type="checkbox" 	name="regex[]" /></td>
				<td><input type="button" class="add" value="Add More" /></td>
			</tr>
		</table>
		<br>
		<label for="number"> Expires(Days[1-30]): </label>
		<input type="number" min="1" max="30" value="" name="days">
		<br><br>
    <button type="submit">SilenceAlert</button>
	  </form>
		<hr>
		<small>User: %s</small>
    </body>
</html>

以上的文件中,当我点击"Add More"按钮时,会添加一行新的表格行,当我点击"Delete"按钮时,会删除一行表格行,这是预期的行为。

我在一个Go语言项目中使用了上述的HTML代码(文件名为main.go):

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

const manage_alertsPage = `
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<script type="text/javascript" src="./jquery-1.7.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){

				$('.del').live('click',function(){
					$(this).parent().parent().remove();
				});

				$('.add').live('click',function(){
					$(this).val('Delete');
					$(this).attr('class','del');
					var appendTxt = "<tr><td><input type='text' name='name[]' /></td> <td><input type='text' name='value[]' /></td> <td><input type='checkbox' name='regex[]' /></td><td><input type='button' class='add' value='Add More' /></td></tr>";
					$("tr:last").after(appendTxt);
				});
			});
		</script>
		<style>
			table {
			  table-layout: fixed;
			  width:1200px;
			}
			input{
			  width:285px;
			}
		</style>
	 </head>

    <body>
		<form method="post" action="/silencealert">
		<table id="options-table">
			<tr>
				<td>Name</td>
				<td>Value</td>
				<td>Regex</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td><input type="text"   name="name[]" /></td>
				<td><input type="text"   name="value[]" /></td>
				<td><input type="checkbox" 	name="regex[]" /></td>
				<td><input type="button" class='del' value='Delete' /></td>
			</tr>
			<tr>
				<td><input type="text"   name="name[]" /></td>
				<td><input type="text"   name="value[]" /></td>
				<td><input type="checkbox" 	name="regex[]" /></td>
				<td><input type="button" class="add" value="Add More" /></td>
			</tr>
		</table>
		<br>
		<label for="number"> Expires(Days[1-30]): </label>
		<input type="number" min="1" max="30" value="" name="days">
		<br><br>
    <button type="submit">SilenceAlert</button>
	  </form>
		<hr>
    </body>
</html>
`

func manage_alertsPageHandler(response http.ResponseWriter, request *http.Request) {
	fmt.Fprintf(response, manage_alertsPage)
}

func silencealertPageHandler(response http.ResponseWriter, request *http.Request) {

	err := request.ParseForm()
	if err != nil {
		// Handle error here via logging and then return
		fmt.Fprintf(response, "There was an error processing form: "+err.Error())
		return
	}
	fmt.Printf("%+v\n", request.Form)

	fmt.Fprintf(response, manage_alertsPage)
}

// server main method

var router = mux.NewRouter()

func main() {

	router.HandleFunc("/manage_alerts", manage_alertsPageHandler)
	router.HandleFunc("/silencealert", silencealertPageHandler)

	http.Handle("/", router)
	http.ListenAndServe(":8000", nil)
}

Go项目的文件夹结构如下:

src/web
- main.go
- jquery-1.7.min.js

当我运行该Go项目时:

go run main.go

在浏览器中打开:

localhost:8000/manage_alerts

点击"Add More"或"Delete"按钮时,没有任何反应。

我需要帮助找出为什么JavaScript代码在这里无法执行,而在Golang代码之外却可以正常执行。

英文:

Following html works standalone

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;./jquery-1.7.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
$(&#39;.del&#39;).live(&#39;click&#39;,function(){
$(this).parent().parent().remove();
});
$(&#39;.add&#39;).live(&#39;click&#39;,function(){
$(this).val(&#39;Delete&#39;);
$(this).attr(&#39;class&#39;,&#39;del&#39;);
var appendTxt = &quot;&lt;tr&gt;&lt;td&gt;&lt;input type=&#39;text&#39; name=&#39;name[]&#39; /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type=&#39;text&#39; name=&#39;value[]&#39; /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type=&#39;checkbox&#39; name=&#39;regex[]&#39; /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&#39;button&#39; class=&#39;add&#39; value=&#39;Add More&#39; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
$(&quot;tr:last&quot;).after(appendTxt);
});
});
&lt;/script&gt;
&lt;style&gt;
table {
table-layout: fixed;
width:1200px;
}
input{
width:285px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;/silencealert&quot;&gt;
&lt;table id=&quot;options-table&quot;&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Value&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot;   name=&quot;name[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; 	 name=&quot;value[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;checkbox&quot; 	name=&quot;regex[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;button&quot; class=&#39;del&#39; value=&#39;Delete&#39; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot;   name=&quot;name[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; 	 name=&quot;value[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;checkbox&quot; 	 name=&quot;regex[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;button&quot; class=&quot;add&quot; value=&quot;Add More&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;label for=&quot;number&quot;&gt; Expires(Days[1-30]): &lt;/label&gt;
&lt;input type=&quot;number&quot; min=&quot;1&quot; max=&quot;30&quot; value=&quot;&quot; name=&quot;days&quot;&gt;
&lt;br&gt;&lt;br&gt;
&lt;button type=&quot;submit&quot;&gt;SilenceAlert&lt;/button&gt;
&lt;/form&gt;
&lt;hr&gt;
&lt;small&gt;User: %s&lt;/small&gt;
&lt;/body&gt;

</html>

With the above file when i click on Add More a new table row gets added and when i click delete a table row gets deleted. which is the expected behaiour.

I am using the above html in a golang project (file - main.go)

package main
import (
&quot;fmt&quot;
&quot;github.com/gorilla/mux&quot;
&quot;net/http&quot;
)
const manage_alertsPage = `
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;./jquery-1.7.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
$(&#39;.del&#39;).live(&#39;click&#39;,function(){
$(this).parent().parent().remove();
});
$(&#39;.add&#39;).live(&#39;click&#39;,function(){
$(this).val(&#39;Delete&#39;);
$(this).attr(&#39;class&#39;,&#39;del&#39;);
var appendTxt = &quot;&lt;tr&gt;&lt;td&gt;&lt;input type=&#39;text&#39; name=&#39;name[]&#39; /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type=&#39;text&#39; name=&#39;value[]&#39; /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type=&#39;checkbox&#39; name=&#39;regex[]&#39; /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&#39;button&#39; class=&#39;add&#39; value=&#39;Add More&#39; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
$(&quot;tr:last&quot;).after(appendTxt);
});
});
&lt;/script&gt;
&lt;style&gt;
table {
table-layout: fixed;
width:1200px;
}
input{
width:285px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;/silencealert&quot;&gt;
&lt;table id=&quot;options-table&quot;&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Value&lt;/td&gt;
&lt;td&gt;Regex&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot;   name=&quot;name[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; 	 name=&quot;value[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;checkbox&quot; 	name=&quot;regex[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;button&quot; class=&#39;del&#39; value=&#39;Delete&#39; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot;   name=&quot;name[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; 	 name=&quot;value[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;checkbox&quot; 	 name=&quot;regex[]&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;button&quot; class=&quot;add&quot; value=&quot;Add More&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;label for=&quot;number&quot;&gt; Expires(Days[1-30]): &lt;/label&gt;
&lt;input type=&quot;number&quot; min=&quot;1&quot; max=&quot;30&quot; value=&quot;&quot; name=&quot;days&quot;&gt;
&lt;br&gt;&lt;br&gt;
&lt;button type=&quot;submit&quot;&gt;SilenceAlert&lt;/button&gt;
&lt;/form&gt;
&lt;hr&gt;
&lt;/body&gt;
&lt;/html&gt;
`
func manage_alertsPageHandler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, manage_alertsPage)
}
func silencealertPageHandler(response http.ResponseWriter, request *http.Request) {
err := request.ParseForm()
if err != nil {
// Handle error here via logging and then return     
fmt.Fprintf(response, &quot;There was an error processing form: &quot; + err.Error()) 
return      
}
fmt.Printf(&quot;%+v\n&quot;, request.Form)
fmt.Fprintf(response, manage_alertsPage)
}
// server main method
var router = mux.NewRouter()
func main() {
router.HandleFunc(&quot;/manage_alerts&quot;, manage_alertsPageHandler)
router.HandleFunc(&quot;/silencealert&quot;, silencealertPageHandler)
http.Handle(&quot;/&quot;, router)
http.ListenAndServe(&quot;:8000&quot;, nil)
}

The folder structure for the go project is

src/web
- main.go
- jquery-1.7.min.js

when i run the go project:

go run main.go

Browse to

localhost:8000/manage_alerts

Click on Add More or Delete button, nothing happens

Need some help to figure out why javascript code is not executing here when it was executing just fine outside of the golang code.

答案1

得分: 1

当您直接在浏览器中打开HTML文件时,浏览器会在您的文件系统中查找jquery文件。当您通过导航到localhost:8000/...查看页面时,浏览器会在localhost:8000/...查找jquery文件。


您的Go应用程序只能处理您注册的这两个路径的请求。它呈现的HTML页面将请求localhost:8000/jquery-1.7.min.js以下载jquery文件,但您没有注册处理此类请求的处理程序...

也就是说,您缺少类似于以下内容的代码:

router.HandleFunc("/jquery-1.7.min.js", serveJQuery)

不过在实际情况中,您可能希望提供多个文件,因此请查看gorilla/mux的自述文件中有关静态文件的更完整解决方案。

英文:

When you open the HTML file directly in the browser, the browser will look for the jquery file in your file system. When you view the page by navigating to localhost:8000/... the browser will look for the jquery file at localhost:8000/....


Your Go app can handle requests to only those two paths that your registered. The html page it renders will make a request to localhost:8000/jquery-1.7.min.js to download the jquery file but you did not register a handler to handle such a request...

That is, you're missing something like this:

router.HandleFunc(&quot;/jquery-1.7.min.js&quot;, serveJQuery)

In the real world though you probably will want to serve more than just a single file, so have a look at gorilla/mux's readme on static files for a more complete solution.

huangapple
  • 本文由 发表于 2017年5月18日 03:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/44032886.html
匿名

发表评论

匿名网友

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

确定