我在一个表单中有两个提交按钮。我如何确定哪一个击中了服务器端?
当前回答
您还可以使用href属性并为每个按钮发送带有附加值的get。但那时就不需要填表格了
href="/SubmitForm?action=delete"
href="/SubmitForm?action=save"
其他回答
一个HTML示例,在不同的按钮点击发送不同的表单动作:
<form action="/login" method="POST">
<input type="text" name="username" value="your_username" />
<input type="password" name="password" value="your_password" />
<button type="submit">Login</button>
<button type="submit" formaction="/users" formmethod="POST">Add User</button>
</form>
使用相同的表单添加新用户和登录用户。
需要注意的是,如果您有多个提交按钮,并且您按了return (ENTER键),那么键盘上的默认按钮值将是DOM上的第一个按钮。
例子:
<form>
<input type="text" name="foo" value="bar">
<button type="submit" name="operation" value="val-1">Operation #1</button>
<button type="submit" name="operation" value="val-2">Operation #2</button>
</form>
如果你在这个表单上按ENTER键,将发送以下参数:
foo=bar&operation=val-1
由于您没有指定使用的服务器端脚本方法,因此我将给出一个适用于PHP的示例
<?php if(isset($_POST["loginForm"])) { print_r ($_POST); // FOR Showing POST DATA } elseif(isset($_POST["registrationForm"])) { print_r ($_POST); } elseif(isset($_POST["saveForm"])) { print_r ($_POST); } else{ } ?> <html> <head> </head> <body> <fieldset> <legend>FORM-1 with 2 buttons</legend> <form method="post" > <input type="text" name="loginname" value ="ABC" > <!--Always use type="password" for password --> <input type="text" name="loginpassword" value ="abc123" > <input type="submit" name="loginForm" value="Login"><!--SUBMIT Button 1 --> <input type="submit" name="saveForm" value="Save"> <!--SUBMIT Button 2 --> </form> </fieldset> <fieldset> <legend>FORM-2 with 1 button</legend> <form method="post" > <input type="text" name="registrationname" value ="XYZ" > <!--Always use type="password" for password --> <input type="text" name="registrationpassword" value ="xyz123" > <input type="submit" name="registrationForm" value="Register"> <!--SUBMIT Button 3 --> </form> </fieldset> </body> </html>
形式
当点击Login -> loginForm
当点击Save -> saveForm
当点击注册->注册表单
如果你给每一个名称,点击的将作为任何其他输入发送。
<input type="submit" name="button_1" value="Click me">
使用formaction HTML属性(第5行):
<form action="/action_page.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<button type="submit">Submit</button><br>
<button type="submit" formaction="/action_page2.php">Submit to another page</button>
</form>
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击