我怎样才能通过提交按钮传递参数? [英] How can I pass a parameter via submit button?
问题描述
在我的一个迷你在线书店的代码中,我有一个下面的行,用'name'参数重复5次不同的值
< input name =JSP-2type =submitvalue =Buy>
点击按钮购买后,应用程序重定向到一个文件buy.jsp,名称并显示该书的相应详细信息。
在我的buy.jsp中,我包含了
<%String bname = request.getParameter(name);
out.print(bname);
%>
但是这个名字并不会被分配给bname,而是显示为空值。如何从提交类型输入中传递参数?
请帮助。
您必须在请求中传递参数。
由于您拥有表单并提交表单,因此您可以在名为submitType的表单中有一个隐藏字段,并在您使用javascript单击按钮时填充它。然后这将在下一个请求中可用。
表单内的某处: 在提交按钮中: Javascript: In my code for an mini online book store i have a following line repeating 5 times with different value for 'name' parameter 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. In my buy.jsp, I have included 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. 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. Somewhere inside the form : in the submit buttons: Javascript:
formName is the name of your form
这篇关于我怎样才能通过提交按钮传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< input type =hiddenname =submitType>
< input name =JSP-2type = submitonclick =setType('Buy')>
formName 是你的表单的名称
< script>
函数setType(type)
{
// formName是表单的名称,submitType是提交按钮的名称。
document.forms [formName]。elements [submitType]。value = type;
//或者,您可以通过它的Id
document.getElementById(submitId)。value = type;
}
< / script>
<input name="JSP-2" type="submit" value="Buy">
<% String bname= request.getParameter("name");
out.print(bname);
%>
<input type="hidden" name="submitType">
<input name="JSP-2" type="submit" onclick="setType('Buy')">
<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>