但是我已经超过了我遇到的第一个绊脚石。 你原来的:x = document.myForm 不起作用。您将不得不使用: x = eval(document.myForm); 或 x = document.forms [''myForm'']; //这个要求表格 //有一个id。 XHTML标准 //需要< form>有一个''id'' //属性,''name''属性 //是不被允许的。 那里我会做一些其他的样式更改,比如 ''if''结构,但是一旦我们有了的功能,我就会担心。 Shawwn < script type =" text / javascript"> function validate(){ var x = document.forms [''myForm'']; varCheckedButton = x.receiveVia.value; varName = x.Name.value; varEmail = x.Email.value; varAddress = x.Address.value; if(varCheckedButton == byEmail){ if(varEmail == - 1){ alert(请填写电子邮件); submitOK =" False" ; } if(varName.length == 0){ alert(你必须输入你的姓名; ) submitOK =" False" } if(submitOK ==" False" ;){ 返回false } }否则{ 如果(varCheckedButton ==打印){ if(varEmail == - 1){ alert(请填写电子邮件) submitOK =" False" } if(varName.length == 0){ alert (您必须输入您的姓名) submitOK =" False" } if(varAddress.length == 0){ 提醒(你必须输入你的地址) submitOK =" False" } } } } < / script> Oleg写道: 你好:我一直试图在一个表格中创建两组不同的必填字段使用 radiobutton作为这些集合之间的切换器。 在我的HTML表单中有两个单选按钮,其值为通过电子邮件和 印刷手册''。 如果用户检查''通过电子邮件''radiobutton,他/她必须填写电子邮件和姓名字段 ,如果检查了'radiobutton''Printed Brochure'',用户必须填写电子邮件,姓名 和ALSO地址字段。 我使用下面的这个剧本,但它似乎没有工作,我不能得到它为什么...... 如果有人能帮助我,我会很感激的用这个。 Heya Oleg, 必须引用直接字符串。未加引号的字符串就像你之前的那样 是一个变量。同样,你还没有声明一个名为 " submitOK"的变量。它不是预定义的变量。如果您打算在条件中声明 ,请注意submitOK 变量的范围仅限于条件块内且未定义 其他地方。你必须在 的最大块中声明变量,你希望它具有范围。假设您打算使用它作为表单停止提交的方式 ,我已经修复了剩下的代码。 请注意,有没有理由检查是否byEmail已经选择了 ,所以我删除了支票: < script type =" text / javascript"> function validate(){ var x = document.myForm; var vCheckedButton = x.receiveVia.value; var vName = x .Name.value; var vEmail = x.Email.value; var vAddress = x.Address.value; if(vEmail.length == 0){ alert(请填写电子邮件); } 否则如果( vName.length == 0){ alert(你必须输入你的姓名); } else if((vCheckedButton) ==" printed")&&(vAddress.length == 0)){ alert(你必须输入你的地址); } else { x.submit(); } } < / script> < form name =" myForm"行动= QUOT;" method =" post" enctype =" x-www-form-urlencoded"> < input type =" radio"名称= QUOT; receiveVia"值= QUOT;印刷" ID = QUOT;印刷" /> < label for =" printed">& nbsp; Printed brochure< / label> < br /> < input type =" radio"名称= QUOT; receiveVia"值= QUOT; byEmail" ID = QUOT; byEmail" /> < label for =" byEmail">& nbsp; Via Email< / label> < br /> < button type =" button"值= [提交" onclick =" validate()"> < img src =" submit.gif"宽度= QUOT; 75"高度= QUOT; 17" alt ="提交按钮" /> < / button> < br /> < / form> < noscript>此表格无法在没有javascript的情况下提交 support< / noscript> 正如Shawn的回复,你应该还使用id 属性和var x = document.getElementById(" myForm")修复表单访问权限。 Hello there: I''ve been trying to create two different sets of required fields inone form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two radiobuttons with values ''Via Email'' and''Printed Brochure''. If a user checks ''Via Email'' radiobutton, he/she has to fill out Emailand Name fields only, if it''s radiobutton ''Printed Brochure'' is checked, a user has tofill Email, Name and ALSO Address field. I use this script below, but it doesn''t seem to work, and I can''t getit why.... I''d appreciate it if somebody would help me with this. <script type="text/javascript">function validate(){x=document.myFormvarCheckedButton=x.receiveVia.valuevarName=x.Name.valuevarEmail=x.Email.valuevarAddress=x.Address.valueif (varCheckedButton==byEmail){if (varEmail==-1){alert("Please fill in Email")submitOK="False"}if (varName.length==0){alert("You must enter your Name")submitOK="False"}if (submitOK=="False"){return false}}else{if (varCheckedButton==printed){if (varEmail==-1){alert("Please fill in Email")submitOK="False"}if (varName.length==0){alert("You must enter your Name")submitOK="False"}if (varAddress.length==0){alert("You must enter your Address")submitOK="False"}}}}</script> <form name="myForm" action="" method="POST"enctype="x-www-form-urlencoded"> <p><input type="radio" name="receiveVia" value="printed">&nbsp;Printedbrochure</p><p><input type="radio" name="receiveVia" value="byEmail">&nbsp;ViaEmail</p> <p><input type="image" src="submit.gif" border="0" value="Submit"width="75" height="17" ALT="Submit button" onClick="validate();"></p> </form> Thanks in advance.Oleg 解决方案 ol******@yahoo.com (Oleg) wrote in message news:<b5**************************@posting.google. com>... Hello there: I''ve been trying to create two different sets of required fields in one form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two radiobuttons with values ''Via Email'' and ''Printed Brochure''. If a user checks ''Via Email'' radiobutton, he/she has to fill out Email and Name fields only, if it''s radiobutton ''Printed Brochure'' is checked, a user has to fill Email, Name and ALSO Address field. I use this script below, but it doesn''t seem to work, and I can''t get it why.... I''d appreciate it if somebody would help me with this. <script type="text/javascript"> function validate() { x=document.myForm varCheckedButton=x.receiveVia.value varName=x.Name.value varEmail=x.Email.value varAddress=x.Address.value if (varCheckedButton==byEmail) { if (varEmail==-1) { alert("Please fill in Email") submitOK="False" } if (varName.length==0) { alert("You must enter your Name") submitOK="False" } if (submitOK=="False") { return false } } else { if (varCheckedButton==printed) { if (varEmail==-1) { alert("Please fill in Email") submitOK="False" } if (varName.length==0) { alert("You must enter your Name") submitOK="False" } if (varAddress.length==0) { alert("You must enter your Address") submitOK="False" } } } } </script> <form name="myForm" action="" method="POST" enctype="x-www-form-urlencoded"> <p><input type="radio" name="receiveVia" value="printed">&nbsp;Printed brochure</p> <p><input type="radio" name="receiveVia" value="byEmail">&nbsp;Via Email</p> <p><input type="image" src="submit.gif" border="0" value="Submit" width="75" height="17" ALT="Submit button" onClick="validate();"></p> </form> Thanks in advance. Oleg Well, part of the problem is that you do not have all of the objectsin the HTML form that you do in the script. Here is what I have sofar. I reformatted a bit so that I could read it more easily. Once you get this to the next step, post the function, and, ifyou''re still having any problems, I''ll take another whack at it.I''d do it now, but I can see that you''re not finished writing ityet, and I got past the first stumbling block I hit. Your original: x=document.myFormdoes not work. You''d have to use either: x = eval(document.myForm); or x = document.forms[''myForm''];//This one requires that the form//have an id. The XHTML standard//requires a <form> to have an ''id''//attribute, and the ''name'' attribute//is disallowed.There are some other style changes I''d make, like the''if'' structure, but I can worry about that once we havefunctionality. Shawwn<script type="text/javascript">function validate(){var x = document.forms[''myForm''];varCheckedButton = x.receiveVia.value;varName = x.Name.value;varEmail = x.Email.value;varAddress = x.Address.value; if (varCheckedButton==byEmail){if (varEmail==-1){alert("Please fill in Email");submitOK="False";} if (varName.length==0){alert("You must enter your Name")submitOK="False"} if (submitOK=="False"){return false} }else{ if (varCheckedButton==printed){ if (varEmail==-1){alert("Please fill in Email")submitOK="False"} if (varName.length==0){alert("You must enter your Name")submitOK="False"} if (varAddress.length==0){alert("You must enter your Address")submitOK="False"}}}}</script>Oleg wrote: Hello there:I''ve been trying to create two different sets of required fields inone form and to use aradiobutton as sort of a switcher between these sets.In my HTML form there are two radiobuttons with values ''Via Email'' and''Printed Brochure''.If a user checks ''Via Email'' radiobutton, he/she has to fill out Emailand Name fieldsonly, if it''s radiobutton ''Printed Brochure'' is checked, a user has tofill Email, Nameand ALSO Address field.I use this script below, but it doesn''t seem to work, and I can''t getit why....I''d appreciate it if somebody would help me with this. Heya Oleg,Direct strings must be quoted. An unquoted "string" like you had beforeis a variable. Likewise, you have not declared a variable called"submitOK" and it is not a predefined variable. If you meant to declareit within the conditionals, note that the scope of the "submitOK"variable is confined to within the conditional block and undefinedelsewhere. You have to declare the variable in the largest block overwhich you want it to have scope. Assuming you meant to use it as a wayfor the form to stop being submitted, I''ve fixed the rest of the code.Note that there''s no reason to check whether "byEmail" has beenselected, so I removed the check: <script type="text/javascript">function validate() {var x=document.myForm;var vCheckedButton=x.receiveVia.value;var vName=x.Name.value;var vEmail=x.Email.value;var vAddress=x.Address.value; if (vEmail.length==0) {alert("Please fill in Email");}else if (vName.length==0) {alert("You must enter your Name");}else if ((vCheckedButton=="printed") && (vAddress.length==0)) {alert("You must enter your Address");}else {x.submit();}}</script> <form name="myForm" action="" method="post"enctype="x-www-form-urlencoded"><input type="radio" name="receiveVia" value="printed" id="printed" /><label for="printed">&nbsp;Printed brochure</label><br /><input type="radio" name="receiveVia" value="byEmail" id="byEmail" /><label for="byEmail">&nbsp;Via Email</label><br /><button type="button" value="Submit" onclick="validate()"><img src="submit.gif" width="75" height="17" alt="Submit button" /></button><br /></form><noscript>This form cannot be submitted without javascriptsupport</noscript>As by Shawn''s reply, you should also fix the form access with an idattribute, and "var x = document.getElementById("myForm")". 这篇关于JS:一种形式的两组必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 12:36