I am trying to make a Ajax form Submission in Jquery. The form itself is loaded via an Ajax Event
The Form Element looks like this
<form id="create_user form" action="http://127.0.0.1/create_user" method="post">
<p>Name
<input type="text" name="name" value="" id="name"></p>
<p>Employee Number:<br>
<input type="text" name="emp_no" value="" id="emp_no"></p>
<p>Email:<br>
<input type="text" name="email" value="" id="email"></p>
<p><input type="submit" name="submit" value="Save"></p>
</form>
When loaded as html it works fine.
However I am trying to load it in another page via Ajax event
So the 开发者_StackOverflow社区result looks like this
<div class="admin_main" id="admin_main">
<form id="create_user form" action="" method="post">
.....Form......
<p><input type="submit" name="submit" value="Save"></p>
</form>
</div>
The Jquery code inside document ready function is
$("#admin_main").delegate("#create_user_form", "submit", function() {
$.post('http://127.0.0.1/create_user', $("#create_user_form").serialize(), function (data) {
$("#admin_main").html(data);
}, "html");
});
However this has the effect of merely reloading the page. I have been at it for hours. Where am I going wrong?
I think you forgot the return: false;
(or event.preventDefault()
/ event.stopPropagation()
- not sure what is needed here) in the submit function. You definitely must stop the submit event somehow.
精彩评论