我在一个表单中有两个提交按钮。我如何确定哪一个击中了服务器端?
当前回答
也许这里建议的解决方案在2009年是有效的,但我已经测试了所有这些被好评的答案,没有一个在任何浏览器上都有效。
我发现唯一有效的解决方案是这样的(但我觉得用起来有点难看):
<form method="post" name="form">
<input type="submit" value="dosomething" onclick="javascript: form.action='actionurl1';"/>
<input type="submit" value="dosomethingelse" onclick="javascript: form.action='actionurl2';"/>
</form>
其他回答
一个更好的解决方案是使用按钮标签提交表单:
<form>
...
<button type="submit" name="action" value="update">Update</button>
<button type="submit" name="action" value="delete">Delete</button>
</form>
按钮内部的HTML(例如..>Update<..是用户所看到的;因为有HTML提供,值是不可见的用户;它只发送到服务器。这样就不会给国际化和多种显示语言带来不便(在前一种解决方案中,按钮的标签也是发送到服务器的值)。
<form>
<input type="submit" value="Submit to a" formaction="/submit/a">
<input type="submit" value="submit to b" formaction="/submit/b">
</form>
您可以在一个表单中设置多个提交按钮 例子:
<input type="submit" name="" class="btn action_bg btn-sm loadGif" value="Add Address" title="" formaction="/addAddress">
<input type="submit" name="" class="btn action_bg btn-sm loadGif" value="update Address" title="" formaction="/updateAddress">
如果你给每一个名称,点击的将作为任何其他输入发送。
<input type="submit" name="button_1" value="Click me">
在HTML5中,你可以在输入字段中使用formaction和formmethod属性
<form action="/addimage" method="POST">
<button>Add image</button>
<button formaction="/home" formmethod="get">Cancel</button>
<button formaction="/logout" formmethod="post">Logout</button>
</form>