Java 如何判断点击了哪个提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/735899/
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 tell which submit button was clicked
提问by crauscher
I have several different submit buttons on my JSP in one form tag that all point to the same servlet. I need to know which submit button was clicked. How can I find out which button was clicked?
我的 JSP 上的一个表单标记中有几个不同的提交按钮,它们都指向同一个 servlet。我需要知道点击了哪个提交按钮。我怎样才能知道点击了哪个按钮?
采纳答案by Maurice Perry
if request.getParameter("button-name") is not null then this is the button that was pressed
如果 request.getParameter("button-name") 不是 null 那么这是被按下的按钮
回答by matt b
Each Submit button should have a different name
:
每个提交按钮应该有不同的name
:
<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">
Then, the name of the input should appear in the parameters sent to wherever the form is posting to, something like
然后,输入的名称应出现在发送到表单发布到的任何位置的参数中,例如
post.jsp?key=value&submit3=&....
回答by Gary Kephart
This is kind of similar to the DispatchAction in Struts. What they do is to have a hidden field, and when you submit the form, have onClick() set the value to specify which action is taken.
这有点类似于 Struts 中的 DispatchAction。他们所做的是有一个隐藏字段,当您提交表单时,让 onClick() 设置值以指定要执行的操作。
<input type="hidden" name="dispatchAction"/>
<input type="submit" value="Edit" onClick="setDispatchAction('edit')">
<input type="submit" value="Delete" onClick="setDispatchAction('delete')">
回答by The Surrican
<button type="submit" name="somename" value="button1">some text</button>
<button type="submit" name="somename" value="button2">some other text</button>
you will have the post variable "somename" set to the according value, no matter the dispalyed value.
无论显示的值如何,您都会将帖子变量“somename”设置为相应的值。