JavaScript FAQ(十一)——表单(Form)

 九、表单

 

1. 验证表单(Validating a Form

Q:我如何在表单数据提交服务器之前进行验证?
A:要验证表单的输入,可以在表单的onSubmit事件处理器中调用你的验证函数。当用户提交表单时,浏览器首先会调用onSubmit事件处理器。事实上,只有这个处理器返回true时,表单才会被提交。在下面的例子中,onSubmit事件处理器验证了用户email地址。(为了简单期间,如果地址中没有空格、包含@,并且@既不在开始也不在结尾,就认为该地址合法。)注意,处理器本身必须包含一个return语句,为了向浏览器返回ture或者false

你的Email:

这个例子使用的代码:

<script language="JavaScript">
<!--
function isValid() {
 var email=document.form1.t1.value;
 if (email.indexOf(' ')==-1
      && 0<email.indexOf('@')
      && email.indexOf('@')+1 < email.length
 ) return true;
 else alert ('Invalid email address!')
 return false;
}
//-->
</script>
<form name=form1
method=post
action="javascript:alert('The form is submitted.')"
onSubmit="return isValid()">
Your email:
<input type=text name=t1 size=20 >
<input type=submit value=Submit>
</form>

2. 组合输入域(Combining Input Fields

Q:我可以只从一个文本域获取一个以上的值从而节省表单的空间吗?

A:可以。例如,你可以显示一个文本输入域和一个选择框。使用这个选择框,用户可以选择要在文本域中输入哪个类型的值。实际上,所有输入值都通过hidden表单元素提交到了服务器。试一下这个例子:

Your name:Your email address:Your country:

这个表单由下面的JavaScript代码创建:

<form name=form1 action="" onSubmit="return validate()">
<input name="name"    type=hidden value="">
<input name="email"   type=hidden value="">
<input name="country" type=hidden value="">
<select name=s1 onChange="refill()">
<option value="name" selected >Your name:
<option value="email"         >Your email address:
<option value="country"       >Your country:
</select>
<input name=t1 type=text   value="" size=20>
<input name=b1 type=submit value="Enter!">
</form>
<script language="JavaScript">
<!--
isFilled=new Array(0,0,0);
oldActiveField="name"; oldIndex=0;
theActiveField="name"; theIndex=0;
theValue='';
theForm=document.form1;
function refill() {
 oldIndex=theIndex;
 theIndex=theForm.s1.selectedIndex;
 oldActiveField=theActiveField;
 theActiveField=theForm.s1.options[theIndex].value;
 theValue=theForm.t1.value;
 eval('theForm.'+oldActiveField+'.value=theValue');
 eval('theForm.t1.value=theForm.'+theActiveField+'.value');
 if (theValue!='') isFilled[oldIndex]=1;
 if (theValue=='') isFilled[oldIndex]=0;
}
function read() {
 oldIndex=theForm.s1.selectedIndex;
 oldActiveField=theForm.s1.options[oldIndex].value;
 theValue=theForm.t1.value;
 eval('theForm.'+oldActiveField+'.value=theValue');
 if (theValue!='') isFilled[theIndex]=1;
 if (theValue=='') isFilled[theIndex]=0;
}
function validate() {
 read();
 for (var k=0; k<isFilled.length; k++) {
  if (!isFilled[k]) {
   theIndex=k;
   theForm.s1.selectedIndex=k;
   theForm.t1.value='';
   theActiveField=theForm.s1.options[k].value;
   if (oldIndex==k) alert ('Please fill in your '+theActiveField)
   return false;
  }
 }
 alert ('You are submitting the following data:'
  +'/nName:    '+theForm.name.value
  +'/nEmail:   '+theForm.email.value
  +'/nCountry: '+theForm.country.value
 )
 return false;
 // Instead of returning false in all cases,
 // a real-life code here would validate the data
 // and return true if the user submitted valid data
}
//-->
</script>

3. 回车键会起作用吗?(Will the Enter key work?

Q:当用户按下Enter键时,我的表单会被提交吗?

A:在多数浏览其中,如果你的表单只有一个文本输入域,按下Enter键会提交表单。(表单也可能会包含其他输入元素,如复选框、下拉选择框、单选按钮、密码输入域、隐藏域等等。)

 

4. 使单选按钮不可选(Disabling a Radio Button)

Q:我如何在一个表单中让一个单选按钮不可用(即使其不可选)?

A:要让一个单选按钮不可选,可以在按钮的INPUT标记中使用onClick事件处理器,如下:

<INPUT type="radio" name="myButton" value="theValue"
onClick="this.checked=false;
alert('Sorry, this option is not available!')">

在下面的例子中,“Courier delivery”选项是不可用的。试一下——你会得到警示框:Sorry, this option is not available!

Delivery method (choose one):
download
regular mail
courier delivery

 

时间: 2024-08-02 09:40:25

JavaScript FAQ(十一)——表单(Form)的相关文章

详解JavaScript中的表单验证

       这篇文章主要介绍了JavaScript中的表单验证,是JS在前端和服务器端通信部分相关的重要知识,需要的朋友可以参考下             表单验证用于发生在服务器,客户端已经输入所有必要的数据,然后按下提交按钮之后.如果一些已被输入的客户端的数据的已在错误形式或者被简单地丢失,则服务器将必须的所有数据发送回客户端,并请求的形式以正确的信息重新提交.这是一个漫长的过程,会增加服务器负担.         JavaScript中,提供了一种方法将其发送到web服务器之前验证客户端

关于javascript正则式表单验证,求大神

问题描述 关于javascript正则式表单验证,求大神 下面的函数组完全不起任何作用啊,随意输入都能跳转2.html,求大神们帮看看问题出在哪里. 解决方案 问题出在: <input type="submit" value="注册">,这种写法不管onsubmit函数返true/false,都会提交表单. 推荐解决方案: <input type="button" value="注册" onclick=&qu

javascript正则式表单验证,求大神帮看看问题出在那。

问题描述 javascript正则式表单验证,求大神帮看看问题出在那. 为什么我function show函数组为什么不起作用,输入错了也能跳转到2.html.提示也没有. 解决方案 show函数怎么写的,一行都看不到.哪有你这样提问的. 解决方案二: 你用的什么浏览器?这个问题改正了么有?http://ask.csdn.net/questions/231266 解决方案三: 你确定提交成功了?你打个断点看看 解决方案四: 因为你把js写在下面了,你把js放在 的上方应该就可以了,祝你好运 解决

用JS动态改变表单form里的action值属性的两种方法_javascript技巧

方法1: <form id="form1" name="form1" method="post" action="../news/index.asp"> <table width="100%" height="43" border="0" cellpadding="0" cellspacing="0">

JavaScript实现重置表单(reset)的方法_javascript技巧

本文实例讲述了JavaScript实现重置表单(reset)的方法.分享给大家供大家参考.具体如下: 下面的代码可以对表单内的输入数据进行重置 <!DOCTYPE html> <html> <head> <script> function formReset() { document.getElementById("frm1").reset(); } </script> </head> <body> &

html-关于表单form的相关问题(基础)

问题描述 关于表单form的相关问题(基础) 我这两天刚开始学习HTML,请求大神们这个效果(里面的"公司所在地"后面的北京和东城)怎么用标记select实现呀~~ 解决方案 javascript省市联动示例 解决方案二: 就是表单类型为select后,指定表单选项, 就能显示这个效果了. 完整的html教程地址:http://www.w3school.com.cn/h.asp 解决方案三: 百度一下标签 解决方案四: <select name="city"

javascript创建动态表单的方法_javascript技巧

本文实例讲述了javascript创建动态表单的方法.分享给大家供大家参考.具体实现方法如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

javascript html5实现表单验证_javascript技巧

表单验证为终端用户检测无效的数据并标记这些错误,是一种用户体验的优化. 下面展现浏览器自带的验证功能也可在移动端中查看: HTML部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-sc

Jquery基于Ajax方法自定义无刷新提交表单Form实例_AJAX相关

本文实例讲述了Jquery基于Ajax方法自定义无刷新提交表单Form的方法.分享给大家供大家参考.具体实现方法如下: Jquery的$.ajax方法可以实现ajax调用,要设置url,post,参数等. 如果要提交现有Form需要写很多代码,何不直接将Form的提交直接转移到ajax中呢. 以前的处理方法: 如Form代码如下: 复制代码 代码如下: <form id="Form1" action="action.aspx" method="pos

Javascript简单改变表单元素背景的方法_javascript技巧

本文实例讲述了Javascript简单改变表单元素背景的方法.分享给大家供大家参考.具体如下: 这里使用Javascript改变表单元素的背景,如改变文本框的背景 function colorChange(formName,formItem){ document.forms[formName.name].elements[formItem.name].style.backgroundColor = "#FFFFFF"; } 希望本文所述对大家的javascript程序设计有所帮助. 以