var captcha_def = 'Please copy the text';
$('input[type="submit"]').click( function(x){
x.preventDefault();
var validated;
$('.validate').each(function(){
var content = $(this).val();
if ( content == '' )
{
$(this).addClass('error');
validated = 'false';
}
else
{$(this).removeClass('error');}
});
$('#conf_email').each( function(){ //Just to check to make sure their is a Email Confirmation
var email = $("#email").val();
var conf_email = $('#conf_email').val();
if (email != conf_email)
{
$('#email, #conf_email').addClass('error');
validated = 'false';
}
else
{
$('#email, #conf_email').removeClass('error');
}
});
var captcha = $('#captcha_input').val();
if (captcha == captcha_def)
{
$('#captcha_input').addClass('error');
validated = 'false';
}
alert(validated);
if (validated === 'false')
{
return false;
}
else
{
return true;
}
});
和heres我的html(验证):
<form id='freeform' name='a' method="post" action="index.php" >
<div id="contact_form">
<h2><span class="small-caps">Send</span> US AN <span class="small-caps">Email</span></h2>
<label for="name_input" class="text">*Your Name:</label><input type="text" name="name" class="text validate" id="name_input" /><br />
<label for="email_input" class="text">*Email:</label><input type="text" name="email" class="text validate email" id="email_input" /><br />
<label for="ta_message" class="textarea">Your Message:</label><br />
<textarea id="ta_message" name="message"></textarea><br />
<label for="captcha_input" class="text"><img src="/images/captchas" width="140" height="30" style="border:0;" alt=" " /></label><input type="text" name="captcha" class="captcha text validate" id="captcha_input" /><br />
<input type="submit" id="submit" value="Submit">
</div>
我一直在敲打这个,并且无法弄清楚它是什么,并且在最后期限迫在眉睫,我正试图找出我搞砸了什么。非常感谢你的帮助!
答案 0 :(得分:3)
为什么在开始时调用x.preventDefault()而根本不进行任何验证?如果验证失败,请阻止默认行为?
答案 1 :(得分:1)
x.preventDefault(); line防止控件的默认行为(在这种情况下,提交)发生。删除该行或在返回false之前移动它并且它应该起作用。