一种形式,两个按钮? [英] One form, two buttons?

查看:73
本文介绍了一种形式,两个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格.我希望它具有两个按钮(buttonA,buttonB).除了保留一个隐藏的输入字段并通过onClick()中的javascript将其值设置为每个字符串的不同字符串之外,还有一种更好的方法来找出单击了哪个按钮?

I have a form. I want it to have two buttons (buttonA, buttonB). Is there a better way to figure out which button was clicked than keeping a hidden input field, and setting its value via javascript in onClick() to a different string for each?

这是本文演示的内容: http://www.java2s.com/Code/Java/JSP/JspformUsingButtons.htm

This is what this article is demonstrating: http://www.java2s.com/Code/Java/JSP/JspformUsingButtons.htm

但是似乎有点不可思议,没有比跳过那些篮球更好的方法了吗?

But it seems a little weird, is there no better way than jumping through those hoops?

谢谢

推荐答案

您可以使用namevalue属性来区分表单上的按钮,然后从服务器读取适当的值.这是一个示例:

You can use the name and value attributes to differentiate the buttons on the form, then read the appropriate value from the server. Here is an example:

​<form method="get" action="test.htm" target="new">
  <button type="submit" name="button" value="1">Button One</button>
  <button type="submit" name="button" value="2">Button Two</button>
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​

单击Button 1时,URL将包含button=1,当然,单击Button 2时,URL将包含button=2.

When Button 1 is clicked, the URL will contain button=1, and of course when Button 2 is clicked, button=2.

很明显,只要两个按钮具有相同的name,就可以使用任何所需的name属性.使用POST而不是GET可以相同.

Obviously, you can use any name property you wish, as long as both buttons have the same name. This will work the same using POST instead of GET.

http://jsfiddle.net/M74xN/

这篇关于一种形式,两个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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