我試圖用jQuery(用戶發送的電子郵件/消息)構建確認腳本到我們的電子郵件地址。在jquery表單確認後發送電子郵件
.php工作正常,但jQuery沒有檢查輸入是否爲空併發送。
任何幫助?
HTML
<form onsubmit="return validateForm()" action="send.php" id="myform" method="post">
<label>Your name: </label>
<input type="text" name="username" placeholder="Type your name" id="username" class="required"/>
<label>Your email: </label>
<input type="text" name="email" placeholder="Type your email address" id="email" class="required"/>
<label>Subject/Title: </label>
<input type="text" name="subject" placeholder="Title/Subject" class="required"/>
<label>Write us something: </label>
<textarea col="99" placeholder="Describe as detailed as possible, what you want us to do, for you. Send what you want and we will reply to you, as soon as possible!" id="message"></textarea>
<input type="submit" name="submit" value="Enviar" id="submit"/>
</form>
<div class="alert alert-warning hidden" id="warning">An error has occured! Please fill your email and message!</div>
<div class="alert alert-success hidden" id="success">Message sent! We will reply to you, as soon as possible!</div>
jQuery的
function validateForm() {
var x = document.forms["myform"]["message"].value;
if (x == null || x == ""){
$("#warning").removeClass("hidden");
$("#warning").addClass("show");
$('#warning').delay(5000).fadeOut(500);
return false;
}
if (validateForm == true){
$("#sucess").removeClass("hidden");
$("#success").addClass("show");
$('#sucess').delay(5000).fadeOut(500);
return true;
}
}
防止默認行爲。嘗試'event.preventDefault();'在點擊提交按鈕時防止默認的提交行爲。 – codeninja
[JQuery表單驗證]的可能重複(http://stackoverflow.com/questions/965967/jquery-form-validation) –
你可以做(例如)一個消息框的按鈕,將$'yourform).data ( '確認',TRUE);'。然後在提交事件'if($(yourform).data('confirmed')){/ * allow * /}'檢查該值。 –