---
title: "JavaScript FAQ（十一）"
author: "txf2004"
date: "2008-12-30T08:42:00Z"
tags: []
slug: "javascript-faq"
draft: false
lastmod: "2008-12-30T08:42:00Z"
id: 989700
url: "https://txf2004.iteye.com/blog/989700"
---

**九、表单**

**1. 验证表单（[Validating a Form](http://www.javascripter.net/faq/validati.htm)）**

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

你的Email:

这个例子使用的代码：

01. &lt;scriptlanguage="JavaScript"&gt;
02. &lt;!--
03. functionisValid(){
04. varemail=document.form1.t1.value;
05. if(email.indexOf('')==-1
06. &&0&lt;email.indexOf('@')
07. &&email.indexOf('@')+1&lt;email.length
08. )returntrue;
09. elsealert('Invalidemailaddress!')
10. returnfalse;
11. }
12. //--&gt;
13. &lt;/script&gt;
14. &lt;formname=form1
15. method=post
16. action="javascript:alert('Theformissubmitted.')"
17. onSubmit="returnisValid()"&gt;
18. Youremail:
19. &lt;inputtype=textname=t1size=20&gt;
20. &lt;inputtype=submitvalue=Submit&gt;
21. &lt;/form&gt;

**2. 组合输入域（[Combining Input Fields](http://www.javascripter.net/faq/combinin.htm)）**

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

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

Your name: Your email address: Your country:

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

01. &lt;formname=form1action=""onSubmit="returnvalidate()"&gt;
02. &lt;inputname="name"type=hiddenvalue=""&gt;
03. &lt;inputname="email"type=hiddenvalue=""&gt;
04. &lt;inputname="country"type=hiddenvalue=""&gt;
05. &lt;selectname=s1onChange="refill()"&gt;
06. &lt;optionvalue="name"selected&gt;Yourname:
07. &lt;optionvalue="email"&gt;Youremailaddress:
08. &lt;optionvalue="country"&gt;Yourcountry:
09. &lt;/select&gt;
10. &lt;inputname=t1type=textvalue=""size=20&gt;
11. &lt;inputname=b1type=submitvalue="Enter!"&gt;
12. &lt;/form&gt;
13. &lt;scriptlanguage="JavaScript"&gt;
14. &lt;!--
15. isFilled=newArray(0,0,0);
16. oldActiveField="name";oldIndex=0;
17. theActiveField="name";theIndex=0;
18. theValue='';
19. theForm=document.form1;
20. functionrefill(){
21. oldIndex=theIndex;
22. theIndex=theForm.s1.selectedIndex;
23. oldActiveField=theActiveField;
24. theActiveField=theForm.s1.options\[theIndex].value;
25. theValue=theForm.t1.value;
26. eval('theForm.'+oldActiveField+'.value=theValue');
27. eval('theForm.t1.value=theForm.'+theActiveField+'.value');
28. if(theValue!='')isFilled\[oldIndex]=1;
29. if(theValue=='')isFilled\[oldIndex]=0;
30. }
31. functionread(){
32. oldIndex=theForm.s1.selectedIndex;
33. oldActiveField=theForm.s1.options\[oldIndex].value;
34. theValue=theForm.t1.value;
35. eval('theForm.'+oldActiveField+'.value=theValue');
36. if(theValue!='')isFilled\[theIndex]=1;
37. if(theValue=='')isFilled\[theIndex]=0;
38. }
39. functionvalidate(){
40. read();
41. for(vark=0;k&lt;isFilled.length;k++){
42. if(!isFilled\[k]){
43. theIndex=k;
44. theForm.s1.selectedIndex=k;
45. theForm.t1.value='';
46. theActiveField=theForm.s1.options\[k].value;
47. if(oldIndex==k)alert('Pleasefillinyour'+theActiveField)
48. returnfalse;
49. }
50. }
51. alert('Youaresubmittingthefollowingdata:'
52. +'\\nName:'+theForm.name.value
53. +'\\nEmail:'+theForm.email.value
54. +'\\nCountry:'+theForm.country.value
55. )
56. returnfalse;
57. //Insteadofreturningfalseinallcases,
58. //areal-lifecodeherewouldvalidatethedata
59. //andreturntrueiftheusersubmittedvaliddata
60. }
61. //--&gt;
62. &lt;/script&gt;

**3. 回车键会起作用吗？（[Will the Enter key work?](http://www.javascripter.net/faq/willthee.htm)）**

**Q**：当用户按下Enter键时，我的表单会被提交吗？

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

4\. 使单选按钮不可选（Disabling a Radio Button）

Q：我如何在一个表单中让一个单选按钮不可用（即使其不可选）？

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

1. &lt;INPUTtype="radio"name="myButton"value="theValue"
2. onClick="this.checked=false;
3. alert('Sorry,thisoptionisnotavailable!')"&gt;

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

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