如果选择Javascript无线电组,则否会显示必填文本字段 [英] With Javascript radio group selection of no brings up a required text field

查看:55
本文介绍了如果选择Javascript无线电组,则否会显示必填文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求单选为是"或否"的表格.选择是将移至下一个必需的输入,选择否将调用必需的文本字段以输入黑白页数.以下是我的JavaScript代码.最后一个功能是我需要帮助的功能.我正在从外部文件调用脚本.我是菜鸟,可以为您提供任何帮助.

I have a form with a required radio selection of yes or no. A selection of yes moves to the next required input, and a selection of no calls a required text field to enter the number of black and white pages. The following is my JavaScript code. The last function is the one I need help with. I am calling the script from an external file. I am a rookie and any help is appreciated.

// JavaScript Document
function Form1_Validator(theForm)
{
var radioSelected = false;
for (i = 0;  i < theForm.All_Color.length;  i++)
{
if (theForm.All_Color[i].checked)
radioSelected = true;
}
if (!radioSelected)
{
alert("Please select one of the \"Please Select Color Status\" options.");
return (false);
}
// check if numbers field is blank
if (theForm.How_Many_Are_BW.value == "")
{
alert("Please enter a value for the \"How many pages are Black & White\" field.");
theForm.How_Many_Are_BW.focus();
return (false);
}
// only allow numbers to be entered
var checkOK = "0123456789";
var checkStr = theForm.How_Many_Are_BW.value;
var allValid = true;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numbers in the \"How many pages are Black & White\" field.");
theForm.How_Many_Are_BW.focus();
return (false);
}
}



这是我正在使用的表格.我要处理的输入是全彩色",是或否,有多少个黑白页面是我要绑定到否"选择的页面.



This is the form I am working with. The input that I am trying to work on is the All Color yes or no, and the how many black and white pages is the one I want to tie to the no selection.

<pre lang="xml"><td height="15" valign="top" class="Text">*All Color?</td><br />
                              <td width="410" height="15" valign="top"><p><br />
                                <label><br />
  <input type="radio" name="All_Color" value="Yes" /><br />
                                  Yes</label><br />
                                <label><br />
                                  <br /><br />
                                  <input type="radio" name="All_Color" value="No" id="All_Color_1" /><br />
                                  No</label><br />
                                </p></td><br />
                              </tr><br />
                            <tr><br />
                              <td height="40" valign="top" class="Text">If no, how many pages are B &amp; W?</td><br />
                              <td height="30" valign="top"><br />
                              <input type="text" name="How_Many_Are_BW" /></td></pre><br />



造成不便之处,敬请见谅,但我对此很陌生.谢谢.



Sorry for any inconvenience but I am pretty new to this. Thanks.

推荐答案

我可以看到一些问题:

#1:在HTML中,您需要预先检查单选按钮之一.
将以下属性恰好添加到其中之一:checked="true".

如果不这样做,则您的初始状态会不一致(既不是,也不是),并可能导致错误.
如果这样做,则不必检查并提醒请选择..."(这也是一种不好的样式).

#2:在两个不同的元素"All_Color"上使用相同的名称.不要希望脚本会找到您的任何元素.

#3:您不会显示脚本函数中引用的HTML表单.因此,再次找不到元素.我建议使用document.GetElementById.

...我认为下一步没有多大意义.您需要从基本行为入手,逐步使其变得更复杂,直到您了解如何以更大的块来进行此类工作为止.从查找所有输入元素并调试它们的状态开始.

祝你好运.
—SA
I can see some problems:

#1: In HTML you need to pre-check one of the radio buttons.
Add the following attribute to exactly one of them: checked="true".

If you don''t do it, your initial state is inconsistent (neither Yes, nor No) and can cause an error.
If you do, you don''t have to check up and alert "Please select..." (which is also a bad style).

#2: You use identical name on two different elements "All_Color". Don''t hope any of your elements will be found by your script.

#3: You don''t show HTML form referenced in your script function. Therefore, again, elements would not be found. I advice using document.GetElementById.

...I don''t see much sense in going any further. You need to start from basic behavior and make it more complex step by step, until you learn how to do such work in bigger chunks. Start from finding all input elements and debugging their status.

Good luck.
—SA


这篇关于如果选择Javascript无线电组,则否会显示必填文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆