submitting form to ajax function, but it's reloading the page as well
my question is probably really simple, but i'll ask anyways.
So I have a submit button in my form that has an onclick="ajax()" and at the same time the action of the form is action=""
I notice at the execution of my form the page it is sill redirecting to 开发者_JS百科whatever is in my action code.
What should I be doing when doing ajax form submissions?
use return false;
onclick="ajax();return false;"
Submit buttons submit your form. Use a regular button. input type="button"
If you're using jQuery do it jQuery's way (do not call the function in the onclick
):
$(document).ready(function () {
$("#buttonId").click(function (event) {
event.preventDefault();
// do submit here
});
});
Or use malsup's AJAX form plugin.
what you need to do is preventDefault
form you ajax function so that the submit doesnt get triggered
so it would look like this:
onclick="ajax(event)"
function ajax(e){
...
e.preventDefault();
}
精彩评论