2010-04-30 83 views
0

您好摔跤這種形式和最後一步(實際提交)讓我撓我的腦袋。我至今的形式:表單提交在javascript

<form id="theForm" method='post' name="emailForm"> 
<table border="0" cellspacing="2"> 
<td>Email <span class="red">*</span></td><td><input type='text'class="validate[required,custom[email]]" size="30"></td></tr> 
<td>First Name:</td><td><input type='text' name='email[first]' id='first_name' size=30></td></tr> 
<tr height="30"> 
<td cellpadding="4">Last Name:</td><td><input type='text' name='email[last]' id='e_last_name' size=30> 
<td>Birthday</td> 
<td><select name='month' style='width:70px; margin-right: 10px'> 
<option value=''>Month</option> 
<option value="1">Jan</option> 
<option value="2">Feb</option> 

.... 

</select><select name='day' style='width:55px; margin-right: 10px'> 
<option value=''>Day</option> 

<option value="1">1</option> 
<option value="2">2</option> 
... 

<option value="31">31</option> 
</select><select name='year' style='width:60px;' > 
<option value=''>Year</option> 

<option value="2007">2007</option> 
<option value="2006">2006</option> 
<option value="2005">2005</option> 
... 
</select> 

<input type='image' src='{{skin url=""}}images/email/signUpButt.gif' value='Submit' onClick="return checkAge()" /> 
<input type="hidden" id= "under13" name="under13" value="No"> 

和檢查年齡,並設置一個cookie /變化顯示

function checkAge() 
{ 

var min_age = 13; 
var year = parseInt(document.forms["emailForm"]["year"].value); 
var month = parseInt(document.forms["emailForm"]["month"].value) - 1; 
var day = parseInt(document.forms["emailForm"]["day"].value); 
var theirDate = new Date((year + min_age), month, day); 
var today = new Date; 

if ((today.getTime() - theirDate.getTime()) < 0) { 

var el = document.getElementById('emailBox'); 
if(el){ 
el.className += el.className ? ' youngOne' : 'youngOne'; 
} 
document.getElementById('emailBox').innerHTML = "<style type=\"text/css\">.formError {display:none}</style><p>Good Bye</p><p>You must be 13 years of age to sign up.</p>"; 
createCookie('age','not13',0) 
return false; 
} 
else { 

createCookie('age','over13',0) 
return true; 
    }} 

,一切都似乎是工作的好..只是缺少類型的腳本如果驗證通過(如果他們通過了年齡問題),則實際提交表單的關鍵步驟。所以我想,這將是包裹在腳本..東西在這裏:

else { 
createCookie('age','over13',0) 
return true; 
} 

可有人請幫我找出我怎麼能處理這個提交?

+0

你真的應該使用'parseInt(number,10)'而不是'parseInt(number)' - 否則以'0'開始你的數字將最終解析爲基地址8.'08'和'09'會被'undefined' – gnarf 2010-04-30 18:13:01

+0

謝謝,但我不確定你在說什麼..你在哪裏看到parseInt(number)? – Zac 2010-04-30 20:18:54

回答

2

你會打電話

var form = document.getElementById('theForm'); 
if(form != null) 
    form.submit(); 

這將數據發送到服務器。

+0

對於密集感到抱歉,但只有在checkAge返回true的情況下才會提交?我仍然只是將行爲附加到正常的表單上嗎? – Zac 2010-04-30 18:09:30

+0

@zac - 您應該在代碼的「已驗證」部分(您在OP中正確標識的部分)中,在成功時提交表單。 – gnarf 2010-04-30 18:15:47

+0

所以這應該工作?它不是那麼簡單:P 其他{var form = document.getElementById('theForm'); if(form!= null) form.submit(); createCookie('age','over13',0) return true; } 和這個

Zac 2010-04-30 18:24:20