繁体   English   中英

在提交上工作

[英]working on a onsubmit

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

<!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>

JavaScript 区分大小写。

改变这个:

Function confirmSubmit(){

为此:

function confirmSubmit(){

使用您当前的代码,您只会收到语法错误,因此在提交时没有任何运行。

请注意:

if (themessage == "You are required to complete the following fields: ") {

如果themessage不只是这样,将返回false; 这意味着它总是会失败。 相反,我建议您执行其他操作来确定是否有未填充的字段,例如 boolean 值(一个var持有truefalse 。)它会导致重复代码,但我确信它可以以某种方式重新分解它不会,比如使用 if/else 链。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM