【问题标题】:Form submit in javascript用javascript提交表单
【发布时间】:2010-04-30 17:58:22
【问题描述】:

您好一直在纠结这个表格,最后一步(实际上是提交)让我摸不着头脑。到目前为止,我的表格是:

<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;
}

有人可以帮我弄清楚如何处理这个提交吗?

【问题讨论】:

  • 你真的应该使用parseInt(number, 10) 而不仅仅是parseInt(number) - 否则以0 开头的号码将最终解析为基数8。0809 将是@987654329 @
  • 谢谢,但我不确定你在说什么......你在哪里看到 parseInt(number) ?

标签: javascript forms-authentication


【解决方案1】:

你会打电话给

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

这会将数据发布到服务器。

【讨论】:

  • 很抱歉很密集,但只有在 checkAge 返回 true 时才会提交?我仍然像往常一样将操作附加到表单吗?
  • @zac - 您应该将其与代码的“已验证”部分(您在 OP 中正确识别)放在一起,成功后 - 提交表单。
  • 所以这应该可以吗?不可能那么容易:P else { var form = document.getElementById('theForm'); if(form != null) form.submit(); createCookie('age','over13',0) 返回 true; } 和这个
    whatcounts.com/bin/listctrl" name="emailForm">
  • 好吧,您可能希望在调用 form.submit() 之前放置您想要执行的任何代码,因为一旦发生这种情况,浏览器就会接管并开始获取响应。
  • 太棒了!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-01-04
  • 2014-01-04
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
相关资源
最近更新 更多