我正在構建一個使用Javascript進行驗證的基本訂閱表單,但它無法正常工作。Javascript訂閱表單驗證錯誤
我的表單代碼:
<form id="pageSubscribeForm">
<div class="pageSubscribeCell">
<input id="pageSubscribeName" type="text" class="pageSubscribeInput" value="Tell Us Your Name" />
</div>
<div class="pageSubscribeCell">
<input id="pageSubscribeEmail" type="text" class="pageSubscribeInput" value="Add Your Email Address" />
</div>
<div class="pageSubscribeCell hidden">
<input id="pageSubscribeSpam" type="text" class="pageSubscribeInput" value="" />
</div>
<div class="pageSubscribeCell">
<div class="pageSubscribeButton">
<div id="pageSubscribeSubmit" class="pageSubscribeAction"></div>
</div>
</div>
</form>
和驗證碼:
$("#pageSubscribeSubmit").click(function() {
var name = $("#pageSubscribeName").val();
var nameok = 0;
var email = $("#pageSubscribeEmail").val();
var emailok = 0;
var spam = $("#pageSubscribeSpam").val();
var spamok = 0;
var dataString = 'name='+ name + '&email=' + email + '&spam=' + spam;
if(name!='Tell Us Your Name'){
nameok = 1
}else{
$("#pageSubscribeName").removeClass("subscribeSuccess").addClass("subscribeError");
}
if(email!='Add Your Your Email Address'){
emailok = 1
}else{
$("#pageSubscribeEmail").removeClass("subscribeSuccess").addClass("subscribeError");
}
if(spam ==''){
spamok = 1
}else{
$("#pageSubscribeSpam").removeClass("subscribeSuccess").addClass("subscribeError");
}
if(nameok == 1 && emailok == 1 && spamok == 1){
$('#pageSubscribeSubmit').hide();
}
當我點擊submit
鏈接我得到加入到我的姓名細胞錯誤類,但電子郵件的細胞上保持不變並且垃圾郵件單元保持隱藏狀態。任何想法爲什麼或如何解決?
如果您只是使用'placeholder =「告訴我們您的姓名」,您當然可以刪除大量代碼' – mplungjan
我目前主要關注的是讓這件事情起作用! –
你可以請你回答我的問題,而不是試圖編輯我的整個代碼? –