为什么我的jQuery表单不重新提交? [英] Why won't my jQuery form resubmit?
问题描述
因此,我已经做过几次了,并且奏效了,但是由于某种原因,我的表格这次不起作用了.当我提交表单时,jQuery使用mail.php进行一次最终验证.然后,它要么提交它,要么以错误回显表单.
So I have done this a few times and it has worked, but for some reason my form won't work this time. When I submit the form jQuery uses mail.php to do one final validation. It then either submits it or echoes the form back with errors.
因此,基本上,当我提交有错误的表单时,它将回显相应的错误,但是当我更正值并尝试重新提交时,它将不会重新提交.
So basically when I submit the form with errors, it will echo it back with the respective errors, but when I correct the values and try to resubmit, it won't resubmit.
这是代码:
<html>
<head>
<title>Contact Form Example</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#email').keypress(function() {
p = {};
p['email'] = document.getElementById("email").value;
$("#emailcheck").load("validate_email.php", p);
});
$('#message').keypress(function() {
p = {};
p['message'] = document.getElementById("message").value;
$("#messagecheck").load("validate_message.php", p);
});
$('#mail').click(function() {
p = {};
p['email'] = document.getElementById("email").value;
p['message'] = document.getElementById("message").value;
$("#body").load("mail.php", p);
});
});
</script>
</head>
<body>
<table style="display: block;" id="body">
<form action="mail.php" method="POST">
<tr>
<td align="right">
<label for="email">Email:</label>
</td>
<td align="left">
<input type="text" name="email" id="email" />
</td>
<td id="emailcheck"></td>
</tr>
<tr>
<td align="right" valign="top">
<label for="message">Message:</label>
</td>
<td align="left">
<textarea rows="5" cols="30" name="message" id="message"></textarea>
</td>
<td id="messagecheck"></td>
</tr>
<tr>
<td></td><td><input id="mail" type="submit" value="Email" onclick="return false;"/></td>
</tr>
</form>
</table>
</body>
推荐答案
就在这里:
$("#body").load("mail.php", p);
您要替换#body
的全部内容.这包括您已将回调绑定到的所有元素,因此所有这些回调都将丢失.可能只剩下onclick="return false;"
作为#mail
的操作,因此您的表单已死.
You're replacing the entire content for #body
. That includes all the elements that you've bound callbacks to so all those callbacks are lost. That probably leaves with just onclick="return false;"
as the action for #mail
so your form is dead.
您可以将回调与 live
或
You can bind your callbacks with live
or delegate
or rebind all your handlers when you replace #body
.
这篇关于为什么我的jQuery表单不重新提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!