我正面臨一個問題。 我已經編寫了用於驗證我的輸入字段的代碼,並對其進行了測試,結果工作正常。然後我實現了使用Jquery Ajax post進行表單提交。現在我的問題是,當我點擊提交按鈕時,我以前實現的驗證功能不再被調用。沒有驗證。在Ajax提交期間Javascript表單驗證失效
同樣的問題張貼在計算器,但它沒有解決我problam 問題
$('#submit').click(function (e){
e.preventDefault();
if(!$("form").valid()) return;
...
});
的一些一個答案,我不知道如何使用上面的代碼
HTML
<div class='well'>
<form action="" method="post" class="contactf" name="contactform" onsubmit="return validd()">
<div class='row'>
<div class='col-sm-4'>
<div class='form-group'>
<label for='fname'>Name</label>
<input type='text' name='namee' class='form-control' />
</div>
<div class='form-group'>
<label for='number'>Number</label>
<input type='number' name='numberr' class='form-control' />
</div>
<div class='form-group'>
<label for='email'>Email</label>
<input type='email' name='emaill' class='form-control' />
</div>
<div class='form-group'>
<label for='subject'>Subject</label>
<input type='text' name='subjectt' class='form-control' />
</div>
</div>
<div class='col-sm-8'>
<div class='form-group'>
<label for='message'>Message</label>
<textarea class='form-control' name='messagee' rows='10'></textarea>
</div>
<div class='text-right'>
<input type='submit' name="contactsubmit" class='btn btn-primary' value='Submit' />
</div>
</div>
</div>
</form>
<div class="thanks" style="display: none;">
<h3>Thanks for Enquiry us..Our Team will contact you as soon as possible</h3>
</div>
</div>
J avascript代碼和Ajax
function validd() {
var nam = document.contactform.namee.value;
var number = document.contactform.numberr.value;
var email = document.contactform.emaill.value;
var subject = document.contactform.subjectt.value;
var message = document.contactform.messagee.value;
if (nam == "") {
alert('Please Fill Your Name');
document.contactform.namee.focus();
return false;
}
if (number == "" || number == null) {
alert('Please Enter Your Number');
document.contactform.numberr.focus();
return false;
}
if (number.length < 9) {
alert('Please Enter Your Number at least 10 Digit');
document.contactform.numberr.focus();
return false;
}
if (email == "" || email == null) {
alert('Enteryour Email');
document.contactform.emaill.focus();
return false;
}
if (subject == "" || subject == null) {
alert('Please Fill Your Subject');
document.contactform.subjectt.focus();
return false;
}
if (message == "" || message == null) {
alert('Please Fill Your Message');
document.contactform.messagee.focus();
return false;
} else {
return true;
}
}
$(document).ready(function() {
$(function() {
$('.contactf').submit(function(e) {
e.preventDefault();
var form = $(this);
var post_url = 'contact-enquiry.php';
var post_data = form.serialize();
$.ajax({
type: 'POST',
url: post_url,
data: post_data,
success: function(msg) {
$(form).fadeOut(80, function() {
form.html(msg).fadeIn().delay(100);
$('.thanks').fadeIn();
});
}
});
});
});
});
我不能見你打電話給你驗證功能? –
@kevinSpaceyIsKeyserSözeonsubmit =「return validd()」
爲什麼你不只是在你的提交函數中調用有效? –