繁体   English   中英

提交带有多个提交按钮的表单

[英]Submit form with multiple submit buttons

当您按Enter键时如何使用jQuery提交表单,但是要通过单击第二个提交按钮进行发送。 这样先提交就忽略了。

<form method="post" action="">
    <input type="text" name="inp">
    <input type="submit" name="first" value="first">
    <input type="submit" name="second" value="second">
</form>

您可以按Enter键,而不是执行第一个提交按钮动作的默认动作,而是强制执行第二个提交按钮动作。

在文本输入中处理按键操作:

<input type="text" name="inp" onkeypress="return runScript(event)"/>

然后定义您的函数runScript

func runScript(e) {
    if(e.keyCode == 13) {
        //Submit action goes here
        document.getElementsByName("second")[0].click()
    }
}

我的理解是,您想单击任一按钮来提交表单,但是要使用第二个按钮单击Enter以提交。

以下是几个选项:

1)更改按钮的显示方式

您可以将第一个按钮实际上移到标记中的第二个按钮,但是可以通过不同的位置来更改其显示方式。 请注意,如果执行此操作,则还应该更新tabIndex。

 .firstButton { position: absolute; width: 80px; } .secondButton { position: absolute; left: 90px; width: 80px; } 
 <form name="theForm" method="post" action=""> <input type="text" tabIndex='1' name="inp"> <div> <input type="submit" tabIndex='2' class='secondButton' name="second" value="second"> <input type="submit" tabIndex='1' class='firstButton' name="first" value="first"> </div> </form> 

2)更改第一个按钮不是提交按钮,并手动处理单击

 function submitForm() { document.theForm.submit(); } 
 <form name="theForm" method="post" action=""> <input type="text" name="inp"> <input type="button" name="first" value="first" onclick="submitForm()"> <input type="submit" name="second" value="second"> </form> 

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM