Html 如何通过提交按钮传递参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3797285/
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
How can I pass a parameter via submit button?
提问by naikaustubh
In my code for an mini online book store i have a following line repeating 5 times with different value for 'name' parameter
在我的迷你在线书店代码中,我有以下一行重复 5 次,其中 'name' 参数的值不同
<input name="JSP-2" type="submit" value="Buy">
On clicking the button Buy, the application redirects to a file buy.jsp where it gets the value of name and displays corresponding details of the book.
单击 Buy 按钮后,应用程序重定向到一个文件 buy.jsp,在那里它获取 name 的值并显示该书的相应详细信息。
In my buy.jsp, I have included
在我的buy.jsp中,我已经包括
<% String bname= request.getParameter("name");
out.print(bname);
%>
But the name doesnt get assigned to bname and it shows the value as null. How do I pass a parameter from the submit type input? Please help.
但是名称没有分配给 bname 并且它显示值为空。如何从提交类型输入传递参数?请帮忙。
回答by Nivas
You have to pass the parameter in the request. Since you are having a form, and submitting the form, you can have a hidden field in the form called, say "submitType", and populate it whenever you click the button, using javascript. Then this will be available in the next request.
您必须在请求中传递参数。由于您有一个表单并提交表单,因此您可以在表单中创建一个隐藏字段,例如“submitType”,并在您单击按钮时使用 javascript 填充它。然后这将在下一个请求中可用。
Somewhere inside the form :<input type="hidden" name="submitType">
表格内的某处:<input type="hidden" name="submitType">
in the submit buttons:<input name="JSP-2" type="submit" onclick="setType('Buy')">
在提交按钮中:<input name="JSP-2" type="submit" onclick="setType('Buy')">
Javascript: formNameis the name of your form
Javascript: formName是表单的名称
<script>
function setType(type)
{
//formName is the name of your form, submitType is the name of the submit button.
document.forms["formName"].elements["submitType"].value = type;
//Alternately, you can access the button by its Id
document.getElementById("submitId").value = type;
}
</script>