在嘗試使用Ajax請求將表單提交到我的php腳本之前,我試圖讓我的表單進行驗證.我查看了stackoverflow并沒有找到有用的東西.
我有3個輸入和一個提交按鈕:
$(document).ready(function() {
function validator(){
return $('form').validate();
}
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
dataType: 'html',
type: 'post',
url: 'mail.php',
data: $('#contact-form').serialize(),
beforeSubmit: validator,
success: function(responseData) {
$('#contact-submit').remove();
$('#messageResponse').fadeIn(1000);
$('#messageResponse').html(responseData);
},
error: function(responseData){
console.log('Ajax request not recieved!');
}
});
// resets fields
$('input#full-name').val("");
$('input#email').val("");
$('textarea#message').val("");
})
});
我應該使用插件嗎?我想要的只是用紅色邊框顏色驗證的名稱,電子郵件和消息.這給了我一個很頭疼的問題,明天我將與客戶會面,以確定網站的最終結果.如果我找不到合適的解決方案,我不會問問題.