简体   繁体   English

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

[英]Submit form with multiple submit buttons

How to use jQuery to submit the form when you press enter, but to be sent by clicking on the second submit button. 当您按Enter键时如何使用jQuery提交表单,但是要通过单击第二个提交按钮进行发送。 So that first submit ignored. 这样先提交就忽略了。

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

You could catch the enter key press, and instead of the default action to execute the first submit button action, force it to execute the second. 您可以按Enter键,而不是执行第一个提交按钮动作的默认动作,而是强制执行第二个提交按钮动作。

Handle a keypress action within the text input: 在文本输入中处理按键操作:

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

Then define your function runScript : 然后定义您的函数runScript

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

My understanding is that you want clicking on either button to submit the form, but hitting enter to submit using the second button. 我的理解是,您想单击任一按钮来提交表单,但是要使用第二个按钮单击Enter以提交。

Here are a couple options: 以下是几个选项:

1) Change how buttons are displayed 1)更改按钮的显示方式

You can move the first button to actually be second in the markup, but change how it is displayed by positioning it differently. 您可以将第一个按钮实际上移到标记中的第二个按钮,但是可以通过不同的位置来更改其显示方式。 Note that you should also update the tabIndex if you do this. 请注意,如果执行此操作,则还应该更新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) Change the first button not be a submit button and manually handle click 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> 

Hope that helps. 希望能有所帮助。

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

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