Form submit called using Link. Need Help
I have tried different ways but no success. Googled but can not find this code working.
I want to submit form by clicking Link. Below is the code. Link Click is called but submit function is bypassing.
<a id="Save" class="pageAddLink" href="#" title="Save P开发者_运维百科age Contents">Save Page</a>
<form action="/Home/Edit" id="first" method="post">
</form>
$(document).ready(function () {
$("a.pageAddLink").live('click', function (ev) {
ev.preventDefault();
//$("form#first").submit(); //Only this is working fine but below code not
$("form#first").submit(function (e) {
e.preventDefault();
alert("form submit called");
});
});
});
Your code is binding the form-submit logic on click of the link when you should do this outside the click event. From what I can tell, you want the link to actually invoke the submit
event of the form, not to declare how the submit
event ought to behave:
$("form#theForm").submit(function(e){
e.preventDefault();
alert("Form Submitted");
});
$("a").live("click", function(e) { // is there a reason we're using $.live()?
e.preventDefault();
$("form#theForm").submit(); // Alerts: Form Submitted
});
Demo: http://jsbin.com/iqegu4/edit
Or even simpler....
<a id="Save"
class="pageAddLink"
href="javascript: $('form#first').submit();return false;"
title="Save Page Contents">Save Page</a>
精彩评论