2011-01-12 34 views
13

我想使用下面的錨提交表單與jQuery的Spring。這是如何完成的?我如何使用錨標籤提交與jQuery的表格

<a target="" title="" class="" href="">Save</a> 

我已經試過這一點,其中requestNew是我的表格:

$(document).ready(function(){ 
    $("a").click(function(){ 
    $("#requestNew").submit(function(){ 
     $.post("../acctRequests", $("#requestNew").serialize()); 
     }); 
    }); 
}); 

它似乎並沒有去任何地方。

回答

20

您正在添加一個新的事件處理程序;所有你需要做的就是引發現有的,以及瀏覽器的原生功能:

$(document).ready(function(){ 
    $("a").click(function(){ 
    $("#requestNew").submit(); 
    }); 
}); 
+0

這一工程...感謝您的幫助 – coder 2011-01-12 22:22:01

4

分配外點擊提交處理程序。然後從點擊呼叫它。

$(document).ready(function(){ 
     // Binds the submit handler to the #requestNew form 
    $("#requestNew").submit(function(){ 
     $.post("../acctRequests", $("#requestNew").serialize()); 
    }); 
    $("a").click(function(e) { 
     $("#requestNew").submit(); // calls the submit handler 
     e.preventDefault(); // Prevents the default behavior of the link 
    }); 
}); 
+0

這是正確的方法。 lonesomeway的答案(接受的)不適用於我的瀏覽器,因爲錨標籤的默認行爲未被超載,重新加載頁面,同時丟失了發佈數據。 – eggmatters 2013-02-07 21:50:42

0

如果添加一個id你的錨,你可以在你的點擊功能提交表單:

<a target="" title="" class="" href="" id="saveButton">Save</a> 
$(document).ready(function(){ 
    $('#saveButton').click(function(){ 
    $("#requestNew").submit(); //if requestNew is the id of your form 
    }); 
}); 

如果你想使用Ajax,這是一個不同的解決方案提交,但我不知道這是根據您的問題

6

請使用

<a href="javascript:document.YOUR_FORM_NAME.submit()"> Submit</a> 

<form name="YOUR_FORM_NAME" action="YOUR_ACTION_PAGE_URL" method=POST> 
      <input type="text" name="YOUR_INPUT_NAME" value="YOUR_INPUT_VALUE"/> 
     </form> 

替換「Y想要什麼OUR_ACTION_PAGE_URL」與您有針對性的行動網址

4

HTML

<form...> 
    ... 
    <a data-role="submit">Save</a> 
</form> 

jQuery的

$(function(){ 
    $("[data-role=submit]").click(function(){ 
     $(this).closest("form").submit(); 
    }); 
}); 
0

最簡單的方法是這樣的:

<a href="#" onclick="submit()">Submit</a>