致力于onsubmit

时间:2011-05-05 10:31:18

标签: javascript

我正在撰写表格。我需要一个onsubmit表单处理程序。并创建验证脚本以确保字段不为空。当我点击提交表单时,我没有任何反应。应该弹出一些让我知道我没有填写的东西。有人能告诉我我做错了什么。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>RSVP</title>
<script type="text/javascript">
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value=="") {
themessage = themessage + " - First Name";
 }
if (document.form.last.value=="") {
themessage = themessage + " -  Last Name";
}
if (document.form.NumberofGuest.value=="") {
themessage = themessage + " -  Number of guest";
}
if (document.form.TimeArriving.value=="") {
themessage = themessage + " -  Time Arriving";
}
if (document.form.email.value=="") {
themessage = themessage + " -  E-mail";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
 }
else {
alert(themessage);
return false;
   }

 Function confirmSubmit(){
var submitForm=window.confirm("Are you sure you want to submit the form?");
if (submitForm == true)
return true;
return false;
  }
  function confirmReset(){
  var resetForm = windows.confirm("Are you sure you want to reset the form?");
  if (resetForm == true)
  return true;
  return false;
   }

  </script>
 </head>
 <body>
 <h2>**RSVP to my Party**</h2>
 <form  name=form method="post" action="mailto:sha1023012@yahoo.com?cc=sha1023102@yahoo.com, subject=RSVP"  enctype="text/plain" onsubmit="return confirmSubmit();" onreset="return confirmReset();">
 <input type=text name="first" size="20"> First Name<BR>
 <input type=text name="last" size="20"> Last Name<BR>

<input type=text name="NumberofGuest" size="20"> Number of Guest<BR>
<input type=text name="TimeArriving" size="20"> Time Arriving<BR>
<input type=text name="email" size="20"> E-Mail<BR><BR>
<input type=button value="Submit Request" onclick="verify();">

<input type=reset value="Clear Form"><br>
</form>

3 个答案:

答案 0 :(得分:0)

JavaScript区分大小写。

改变这个:

Function confirmSubmit(){

改为:

function confirmSubmit(){

使用您当前的代码,您只需获得语法错误,因此在传播时没有任何内容正在运行。

答案 1 :(得分:0)

请注意:

if (themessage == "You are required to complete the following fields: ") {
如果themessage等于其他任何东西,

将返回false;这意味着它总会失败。相反,我建议您做其他事情以确定是否存在未填充的字段,例如布尔值(var持有 true false 。)它将导致重复的代码,但我确信它可以以不使用if / else链的方式重新计算。

答案 2 :(得分:-1)

您应该将属性action设置为action="javascript:",以避免默认表单提交。

然后将按钮设置为type="submit"而不是button

最后在提交按钮上将操作设置为onclick。当用户点击进入或单击提交按钮时,将触发该事件。