JS:一种形式的两组必填字段 [英] JS: two sets of required fields in one form

查看:46
本文介绍了JS:一种形式的两组必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:


我一直试图在

一个表格中创建两组不同的必填字段并使用


radiobutton作为这些集合之间的切换器。


在我的HTML表单中有两个单选按钮,其值为通过电子邮件和

''印刷宣传册''。


如果用户检查''通过电子邮件''radiobutton,他/她必须填写电子邮件

和名称字段

仅限
,如果检查了'radiobutton''Printed Brochure'',则用户必须

填写电子邮件,姓名


和ALSO地址字段。


我在下面使用这个脚本,但它似乎没有用,我可以'这不是为了什么......


如果有人帮我这个,我会很感激。

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

{

提醒(请填写电子邮件)

submitOK =" False"

}

if(varName.length == 0)

{

alert(你必须输入你的姓名)

submitOK =" False" ;

}

if(submitOK ==" False")

{

return false
}

}

else

{

if(varCheckedButton == printed)

{

if(varEmail == - 1)

{

alert(请填写电子邮件))

submitOK =" False"

}

if(varName.length == 0)

{

alert(你必须输入你的姓名)

submitOK =" False"

}

if (varAd dress.length == 0)

{

alert(你必须输入你的地址)

submitOK =" False" < br $> b $ b}

}

}

}

< / script>


< form name =" myForm"行动= QUOT;" method =" POST"

enctype =" x-www-form-urlencoded">


< p>< input type =" ;无线电"名称= QUOT; receiveVia" value =" printed">& nbsp;已打印

宣传册< / p>

< p>< input type =" radio"名称= QUOT; receiveVia" value =" byEmail">& nbsp; Via

电子邮件< / p>


< p>< input type =" image" ; SRC =" submit.gif"边界=" 0" value =" Submit"

width =" 75" height =" 17"


ALT =" Submit button" onClick =" validate();">< / p>


< / form>


提前致谢。

Oleg

解决方案

ol******@yahoo.com (奥列格)在留言中写道:< b5 ************************ **@posting.google。 com> ...

你好:

我一直在尝试用一种形式创建两组不同的必填字段并使用<

在我的HTML表单中,有两个单选按钮,其值为通过电子邮件发送和打印小册子。

在我的HTML表单中有两个单选按钮。 ''。

如果用户检查''通过电子邮件''radiobutton,他/她必须填写电子邮件
和姓名字段

,如果它用户必须填写电子邮件,姓名

和ALSO地址栏。

我用这个脚本''''''''''''''''''''''''''''''''''''''''''''''''''''''''下面,但它似乎没有工作,我不能得到它为什么......

如果有人能帮我这个,我会很感激的。

< script type =" text / javascript">
function validate()
{
x = document.myForm
varCheckedButton = x.receiveVia.value
varName = x.Name.va lue
varEmail = x.Email.value
varAddress = x.Address.value
if(varCheckedButton == byEmail)
{
if(varEmail == - 1 )
{
警告(请填写电子邮件)
提交确认= =错误
}如果(varName.length == 0)
{
警告(您必须输入您的姓名)
提交确认= =错误
}
if(submitOK ==" False")
{
返回错误
}
}
如果(varCheckedButton ==打印)
{
如果( varEmail == - 1)
{
警告(请填写电子邮件)
submitOK =" False"
}
if(varName.length = = 0)
{
警告(你必须输入你的姓名)
提交确认= =错误
}如果(varAddress.length == 0 )
{
警告(你必须输入你的地址)
提交确认=错误
}
}
}
} < / script>

< form name =" myForm"行动= QUOT;" method =" POST"
enctype =" x-www-form-urlencoded">

< p>< input type =" radio"名称= QUOT; receiveVia" value =" printed">& nbsp; Printed
宣传册< / p>
< p>< input type =" radio"名称= QUOT; receiveVia" value =" byEmail">& nbsp; Via
电子邮件< / p>

< p>< input type =" image" SRC =" submit.gif"边界=" 0" value =" Submit"
width =" 75" height =" 17"

ALT =" Submit button" onClick =" validate();">< / p>

< / form>

提前致谢。
Oleg



好​​吧,部分问题是你没有在脚本中使用HTML表单中的所有对象

。这就是我所拥有的那样

远。我重新格式化了一下,以便我可以更轻松地阅读它。


一旦你进入下一步,发布功能,如果

你我仍然有任何问题,我会再次打击它。

我现在就做,但我可以看到你没有完成它的写作
但是我已经超过了我遇到的第一个绊脚石。


你原来的: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>


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

解决方案

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 objects
in the HTML form that you do in the script. Here is what I have so
far. I reformatted a bit so that I could read it more easily.

Once you get this to the next step, post the function, and, if
you''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 it
yet, and I got past the first stumbling block I hit.

Your original: x=document.myForm
does 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 have
functionality.

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


Heya Oleg,
Direct strings must be quoted. An unquoted "string" like you had before
is a variable. Likewise, you have not declared a variable called
"submitOK" and it is not a predefined variable. If you meant to declare
it within the conditionals, note that the scope of the "submitOK"
variable is confined to within the conditional block and undefined
elsewhere. You have to declare the variable in the largest block over
which you want it to have scope. Assuming you meant to use it as a way
for 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 been
selected, 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 javascript
support</noscript>


As by Shawn''s reply, you should also fix the form access with an id
attribute, and "var x = document.getElementById("myForm")".


这篇关于JS:一种形式的两组必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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