【发布时间】:2011-05-25 07:25:47
【问题描述】:
我有一个表单,您可以将数据添加到数据库中。这一切都是用 jquery 和 ajax 完成的,所以当你按下提交时,它会验证代码,然后如果一切正确,它会提交发布数据而不刷新页面。问题是表单第一次工作,但是当你用表单提交另一个条目时它不起作用。我认为这与它有关
$(document).ready(function(){
但我真的不知道。我在下面粘贴了一些代码。它很长,但这应该提供足够的信息来了解它在做什么。 整个js文件在http://www.myfirealert.com/callresponse/js/AddUser.js
$(document).ready(function(){
$('#AddCaller').click(function(e){
//stop the form from being submitted
e.preventDefault();
/* declare the variables, var error is the variable that we use on the end
to determine if there was an error or not */
var error = false;
var Firstname = $('#Firstname').val();
...OTHER FORM FIELDS HERE
/* in the next section we do the checking by using VARIABLE.length
where VARIABLE is the variable we are checking (like name, email),
length is a javascript function to get the number of characters.
And as you can see if the num of characters is 0 we set the error
variable to true and show the name_error div with the fadeIn effect.
if it's not 0 then we fadeOut the div( that's if the div is shown and
the error is fixed it fadesOut. */
if(Firstname.length == 0){
var error = true;
$('#Firstname_error').fadeIn(500);
}else{
$('#Firstname_error').fadeOut(500);
}
if(Lastname.length == 0){
var error = true;
$('#Lastname_error').fadeIn(500);
}else{
$('#Lastname_error').fadeOut(500);
}
...MORE CONDITIONAL STATEMENTS HERE
//now when the validation is done we check if the error variable is false (no errors)
if(error == false){
//disable the submit button to avoid spamming
//and change the button text to Sending...
$('#AddCaller').attr({'disabled' : 'true', 'value' : 'Adding...' });
/* using the jquery's post(ajax) function and a lifesaver
function serialize() which gets all the data from the form
we submit it to send_email.php */
$.post("doadd.php", $("#AddCaller_form").serialize(),function(result){
//and after the ajax request ends we check the text returned
if(result == 'added'){
//$('#cf_submit_p').remove();
//and show the success div with fadeIn
$('#Add_success').fadeIn(500);
$('#AddCaller').removeAttr('disabled').attr('value', 'Add A Caller');
document.getElementById('Firstname').value = "";
document.getElementById('Lastname').value = "";
document.getElementById('PhoneNumber').value = "";
document.getElementById('DefaultETA').value = "";
document.getElementById('Apparatus').value = "";
document.getElementById('DefaultLocation').value = "";
setTimeout(" $('#Add_success').fadeOut(500);",5000);
}else if(result == 'alreadythere'){
//checks database to see if the user is already there
$('#Alreadythere').fadeIn(500);
$('#AddCaller').removeAttr('disabled').attr('value', 'Add A Caller');
}
else{
//show the failed div
$('#Add_fail').fadeIn(500);
//reenable the submit button by removing attribute disabled and change the text back to Send The Message
$('#AddCaller').removeAttr('disabled').attr('value', 'Send The Message');
}
});
}
});
});
现在,您第一次使用该表单时效果很好。并且该按钮被重新启用,但是当您尝试进行另一个条目并单击该按钮时,没有任何反应。
感谢您的帮助!
编辑:第一次提交表单后,按钮仍处于启用状态,您可以单击它,但是当您单击它时,没有任何反应...即使您不填写表格。就像表单的点击事件不是第一次触发一样。
EDIT2 根据要求,我将发布 HTML,它位于受密码保护的站点后面,因此我无法向您发送页面链接。
<form action='addcallers.php' method='post' id='AddCaller_form'>
<h2>Add Callers</h2>
<p>
First Name:
<div id='Firstname_error' class='error'> Please Enter a First Name</div>
<div><input type='text' name='Firstname' id='Firstname'></div>
</p>
<p>
Last Name:
<div id='Lastname_error' class='error'> Please Enter a Last Name</div>
<div><input type='text' name='Lastname' id='Lastname'></div>
</p>
...MORE FORM FIELDS HERE
<div style="display:none;">
<input type='text' name='DefaultLocation' id='DefaultLocation' value= "Sometthing" readonly=readonly >
</div>
</p>
<p>
<div id='Add_success' class='success'> The user has been added</div>
<div id='Alreadythere' class='error'> That user is already in the database</div>
<div id='Add_fail' class='error'> Sorry, don't know what happened. Try later.</div>
<p id='cf_submit_p'>
<input type='submit' id='AddCaller' value='Send The Message'>
</p>
</form>
</div>
EDIT3 页面上还有其他 ajax,但它是直接用 javascript 编写的。我不确定这是否会以任何方式影响功能。但如果需要,我也可以发布该 ajax。
EDIT4我从http://web.enavu.com/tutorials/create-an-amazing-contact-form-with-no-ready-made-plugins/得到了原始教程并修改了它
编辑 在放入一些不同的警报后,我发现它不执行条件语句if(error==false)... 任何想法为什么?
【问题讨论】:
-
快速浏览,这应该可以正常工作。在关键行中添加警报,看看它在哪里停止或更好地使用 Firebug 或类似的东西对其进行调试,然后让我们知道结果。
-
是的 - 特别是,我会使用“alert()”或“console.log()”来确保在第二次通过时调用“click”处理程序。
-
第二次没有调用点击事件。你可以随心所欲地点击按钮,但它什么也没做
-
很有趣!你能把html也发一下吗?
-
@Spiny 我为上面的表单添加了 HTML
标签: php javascript jquery ajax forms