我想无需刷新页面提交表单,从我读它应与阿贾克斯的工作,我究竟做错了什么?
I want to submit a form without refreshing the page, from what I have read it should work with ajax, what am i doing wrong?
这一切与PHP和东西的作品,当我做到这一点:
it all works with the php and stuff when I do this:
document.getElementById("msg_form").submit();
但我希望它无需刷新页面提交。
But I want it to submit without refreshing the page.
的HTML部分:
<form name="msg_form_name" id="msg_form" class="email" action="mailer.php">
<p>Your E-mail:</p>
<input id="email_form" name="email" type="text" />
<p>Amount:</p>
<input id="amount_form" name="amount" class="amount_num" type="text" maxlength="5" />
<div id="msg_txt_lenght">characters left: 38</div>
<p>Message:</p>
<input id="message_form" name="message" class="message_form_lim" type="text" >
<input type="hidden" id="storeUrl_id" name="storeUrl_form" value="Nan"></form>
</form>
的JavaScript部分:
Part of javascript:
$('#msg_form').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'mailer.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
return false;
});
在此先感谢。
Thanks in advance.
编辑:可能有固定它有它提交但未发出提交
might have fixed it had it on submit but didn't send a submit
解决了刚更换:
$('#msg_form').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'mailer.php',
data: $('#msg_form').serialize(),
success: function () {
alert('form was submitted');
}
});
return false;
});
本,而不是
$.post('mailer.php', $('#msg_form').serialize())