2016-11-28 92 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>

希望有所幫助。