[英]A Switch Within A Javascript Function
I am in the process of writing a contact form. 我正在写联系表。
I have two Javascript functions in the head (will move them later, but that is another question). 我的头中有两个Javascript函数(稍后会移动它们,但这是另一个问题)。
The first function, Validate()
, for the onblur event is not working. 用于onblur事件的第一个函数
Validate()
不起作用。 The second function, formValidate()
, for the onsubmit event works. 用于onsubmit事件的第二个函数
formValidate()
起作用。
I want to use a switch statement in a javascript function to use in an html onblur event. 我想在javascript函数中使用switch语句以在html onblur事件中使用。
The Javascript: Javascript:
<head>
<script type="text/javascript">
function Validate()
{
// create array containing textbox elements
var input = [document.getElementById('fname'), document.getElementById('lname'), document.getElementById('email1'), document.getElementById('email12'), document.getElementById('message')];
for(var i = 0; i<input.length; i++)
// loop through each element to see if value is empty
{
if(input[i].value == '')
{
switch (ID){
case 'fname':
alert ('enter you first name');
break;
case 'lname' :
alert ('enter your last name');
break;
case 'email1':
alert ('enter your email address');
break;
case 'email2':
alert ('enter your email address');
break;
case 'message':
alert ('write your message');
break;
}
}
}
}
function formValidate()
{
// create array containing textbox elements
var inputs = [document.getElementById('fname'), document.getElementById('lname'), document.getElementById('email1'), document.getElementById('email2'), document.getElementById('message')];
var error;
for(var i = 0; i<inputs.length; i++)
// loop through each element to see if value is empty
{
if(inputs[i].value == '')
{
error = 'Please complete all fields.';
alert(error);
return false;
}
}
}
</script>
</head>
The html: 的HTML:
<form onsubmit="return formValidate()" action="mailto:admin@sfasc.com.au" method="post" id="contactForm" >
<fieldset>
<dl>
<dt> First Name:</dt>
<dd>
<input class="input" type="text" name="fname" id="fname" onblur="Validate()" />
</dd>
<dt> Last Name:</dt>
<dd>
<input class="input" type="text" name="lname" id="lname" onblur="Validate()"/>
</dd>
<dt> Email Address:</dt>
<dd>
<input class="input" type="text" name="email1" id="email1" onblur="Validate()"/>
</dd>
<dt> Email Address:</dt>
<dd>
<input class="input" type="text" name="email2" id="email2" onblur="Validate()"/>
</dd>
<dt> Message:</dt>
<dd>
<textarea name="address" id="address" rows="10" cols="10" onblur="Validate()"></textarea>
</dd>
<dd>
<input type="submit" value="submit" name="submit" />
</dd>
</dl>
</fieldset>
</form>
I have tried removing the second function, but it makes no difference. 我尝试删除第二个功能,但没有区别。 I am only learning.
我只是在学习。
Can anyone, please, tell me what I've done wrong? 谁能告诉我我做错了什么?
This won't work, ID
is not defined anywhere: 这行不通,
ID
任何地方都没有定义:
switch (ID) {
You should replace it with this: 您应该将其替换为:
switch(input[i].id) {
You should do some debugging, the error would surely appear in the console! 您应该进行一些调试,该错误一定会出现在控制台中!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.