所以我陷入了死衚衕。我有AJAX腳本和jQuery驗證插件。有了這個腳本,當我的頁面加載空時,聯繫表單會自動提交。如果我移動或添加$('#submit').click(function() {
而不是AJAX腳本部分,則在$(document).ready(function() {
之下,然後提交它將忽略我的驗證表單。AJAX和jQuery驗證問題
任何幫助將不勝感激。
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script type='text/javascript' src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js'></script>
<script>
var captcha_a = Math.ceil(Math.random() * 10);
var captcha_b = Math.ceil(Math.random() * 10);
var captcha_c = captcha_a + captcha_b;
function generate_captcha(id){
var id = (id) ? id : 'lcaptcha';
$("#"+id).html(captcha_a + " + " + captcha_b + " = ");
}
$(document).ready(function() {
$('#submit').click(function() {
jQuery.validator.addMethod("math",
function(value, element, params) { return this.optional(element) || value == params[0] + params[1]; },
jQuery.format("Please enter the correct value for {0} + {1}")
);
$("#commentform").validate({
rules: {
captcha: {
math: [captcha_a, captcha_b]
}
}
});
});
})
</script>
<script>
$(document).ready(function() {
var name = $('input[name=name]');
var phone = $('input[name=phone]');
var email = $('input[name=email]');
var reason = $('select[name=reason]');
var info = $('textarea[name=info]');
var data = 'name=' + name.val() + '&phone=' + phone.val() + '&email=' + email.val() +'&reason=' + reason.val() + '&info=' + encodeURIComponent(info.val());
$('.loading').show();
$.ajax({
url: "ajaxformtoemail.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
if (html==1) {
$('.form').fadeOut('slow');
$('.done').fadeIn('slow');
} else alert('Sorry, unexpected error. Please try again later.');
}
});
return false;
});
</script>
爲了便於閱讀,我縮進並格式化了您的代碼。 – Sparky 2012-02-27 06:52:59