2016-11-28 93 views
0

如何在按下回车键时使用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> 
+1

我不确定为什么你会有第一个提交按钮,如果它不提交表单。 – Steve

+0

正如我理解你的问题,情况已经如此。按enter键,使用第一个按钮。点击第二个,然后第二个是使用/ submited –

+0

我认为你不需要第一次提交按钮,只是谷歌触发事件键时,按下,你会得到一堆东西读取http:// stackoverflow。 com/questions/18160342/jquery-how-to-trigger-click-event-on-press-enter-key。 – rockStar

回答

0

你可以赶上输入按键,并且代替默认操作执行第一提交按钮行动,迫使其执行第二。

处理文本输入中的按键动作:

<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() 
    } 
} 
0

我的理解是,你要点击任一按钮提交表单,但打输入使用第二个按钮提交。

这里有几个选项:

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>

希望有所帮助。