我需要在Go代码(v.Hostname)中读取Javascript变量(JS2GO)的值。我不知道怎么做。
我读过一些关于syscall/js的文章,但什么也找不到。
这是Javascript代码:
$(document).on("keypress", "input", function(e){
if(e.which == 13){
var JS2GO = $(this).val();
}
});这是我的.gohtml文件中的表单。
<form method="POST">
<input id="myInput" type="text" name="host">
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1">
</form>附注:
我想出了这个解决方案,因为我不能点击"submit“按钮(它是隐藏的),然后它就不会将结果发送给我的Go代码。这意味着我不能通过以下代码:input读取v.Host = r.FormValue("host")的值。如果我的方法不是最佳的,请告诉我。
发布于 2019-09-02 13:23:12
基于对@G.aziz的评论,我找到了一个提到这里的解决方案。在我的例子中,它是这样解决的:
<form method="POST">
<input id="myInput" type="text" name="host" style="min-width: 220px;" placeholder="Hostname; e.g. crowe.org">
<input id="submitBtn" type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1">
</form>以及Ajax代码:
$(document).ready(function () {
$('#submitBtn').click(function () {
$.ajax({
url: '/',
type: 'POST',
dataType: 'HTML',
data : {
ajax_post_data: $('#myInput').val()
},
});
});
});然后在我的Go文件中,我可以得到这样的值
if r.Method == "POST" {
v.Host = r.FormValue("ajax_post_data")
}https://stackoverflow.com/questions/57731559
复制相似问题