2010-07-23 191 views
4

我有兩個提交按鈕和一個表單。我如何檢查在我的jQuery代碼中選擇了哪個提交按鈕?如何處理一個表單上的兩個提交按鈕?

<% using (Html.BeginForm("UserInfo", "Home", FormMethod.Post, new { id = "formNext" })) { %> 
.... 
<input id="submitHome" type="submit" name="goHome" value="Home" /> 
<input id="submitNext" type="submit" name="getNext" value="Next" /> 
<% } %> 


$(document).ready(function() {  
$('#formNext').submit(function() {  
     //Code Does not work but looking at something like this... 
     //$('#submitHome').click(function() { 
     //  navigate to Home; 
     //}); 
     //$('#submitNext').click(function() { 
     //  return true; 
     //}); 
    }); 
}); 
+0

這是否老問題幫幫忙? http://stackoverflow.com/questions/2154039/jquery-submit-how-can-i-know-what-submit-button-was-pressed – Tobiasopdenbrouw 2010-07-23 19:18:01

+0

你應該使用按鈕,而不是提交輸入。 – recursive 2010-07-23 19:33:46

+0

爲什麼你刪除你的[最後一個問題](http://stackoverflow.com/questions/3366104/compatibility-view-kills-my-style-sheet-what-to-do)? – BalusC 2010-07-29 19:32:40

回答

15
$('#submitHome').click(function() { 
     //navigate to Home; 
}); 
$('#submitNext').click(function() { 
     return true; 
}); 

如果你拉他們form.submit以外這些應該工作()。 (表單提交後,現在的處理程序被連接,這是爲時已晚,因爲已經發生了點擊)

+0

這正是我所做的錯誤,謝謝! – MrM 2010-07-23 19:38:35

+0

如果用戶使用鍵盤提交,這不起作用 – Mika 2015-01-12 15:00:02

2

這應該工作:

<% using (Html.BeginForm("UserInfo", "Home", FormMethod.Post, new { id = "formNext" })) { %> 
.... 
<input id="submitHome" type="submit" name="goHome" value="Home" /> 
<input id="submitNext" type="submit" name="getNext" value="Next" /> 
<% } %> 


$(document).ready(function() { 
    $('#submitHome').click(function(e) { 
    e.preventDefault(); // prevent the page to do the usuall onclick event (from reloading the page) 
    // navigate to Home; 
    }); 
    $('#submitNext').click(function(e) { 
    e.preventDefault(); 
    // return true; 
    }); 
}); 
0

創建兩個行動的方法和seperately處理兩個崗位將formcollection作爲參數。 使用type =按鈕添加兩個輸入,並使用$ .post將click事件綁定到您的操作方法,並使用該帖子所需的表單控件中的值。

相關問題