将 html 页面中的 javascript 变量传递到 html 表单(在同一页面上)以提交给 perl cgi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17364576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:02:11  来源:igfitidea点击:

pass a javascript variable within a html page into a html form (on the same page) for submission to perl cgi

javascripthtmlperlcgi

提问by user2531749

<script type="text/javascript">
var id = getValue("ID");
document.write(id);
</script>    

<form action="cgi-bin/runalg.cgi" method="post" enctype="multipart/form-data">
<input type="radio" name="genome" value="E.Coli"> <i>E.Coli</i> <input type="radio" name="genome" value="Human"> Human<br> 
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="reset" value="Clear"/>
</form>

</body>

How do I get the value of the JavaScript variable (var id) into the form so that on submission I can retrieve it in the runalg.cgiusing the $q<-param("ID")command?

如何在表单中获取 JavaScript 变量 (var id) 的值,以便在提交时可以在runalg.cgiusing$q<-param("ID")命令中检索它?

回答by vladkras

you can write it directly:

你可以直接写:

<script type="text/javascript">
document.write('<input type="hidden" name="ID" value="'+id+'"/>');
</script>

回答by Trylks

Add a hidden field. Set the value in that field to the value of the variable in Javascript.

添加隐藏字段。将该字段中的值设置为 Javascript 中变量的值。

<form action="cgi-bin/runalg.cgi" method="post" enctype="multipart/form-data">
[...]
<input type="hidden" name="ID" value="default">
</form>

And then on javascript:

然后在 javascript 上:

<script type="text/javascript">
document.forms[0].elements["ID"].value = getValue("ID");
</script>

The index for the form may vary in your document.

表格的索引在您的文档中可能有所不同。