我怎样才能通过提交按钮传递参数? [英] How can I pass a parameter via submit button?

查看:592
本文介绍了我怎样才能通过提交按钮传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个迷你在线书店的代码中,我有一个下面的行,用'name'参数重复5次不同的值

 < input name =JSP-2type =submitvalue =Buy> 

点击按钮购买后,应用程序重定向到一个文件buy.jsp,名称并显示该书的相应详细信息。



在我的buy.jsp中,我包含了

 <%String bname = request.getParameter(name); 
out.print(bname);
%>

但是这个名字并不会被分配给bname,而是显示为空值。如何从提交类型输入中传递参数?
请帮助。

解决方案

您必须在请求中传递参数。
由于您拥有表单并提交表单,因此您可以在名为submitType的表单中有一个隐藏字段,并在您使用javascript单击按钮时填充它。然后这将在下一个请求中可用。


表单内的某处:

< input type =hiddenname =submitType>


在提交按钮中:
< input name =JSP-2type = submitonclick =setType('Buy')>



Javascript:
formName 是你的表单的名称

 < script> 
函数setType(type)
{
// formName是表单的名称,submitType是提交按钮的名称。
document.forms [formName]。elements [submitType]。value = type;

//或者,您可以通过它的Id
document.getElementById(submitId)。value = type;
}
< / script>


In my code for an mini online book store i have a following line repeating 5 times with different value for 'name' parameter

<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.

In my buy.jsp, I have included

    <% 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.

解决方案

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 :
<input type="hidden" name="submitType">

in the submit buttons:
<input name="JSP-2" type="submit" onclick="setType('Buy')">

Javascript: formName is the name of your form

<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>

这篇关于我怎样才能通过提交按钮传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆