我正在使用 HTML 和 JavaScript 开发一个网站。当我运行我的代码时,它确实会运行,但是无论输入什么,我的 if else 语句都不会遇到 else 情况。有人可以告诉我出了什么问题。我是 Forms 和 javaScript 的新手,但我认为 JavaScript 的大部分内容都与 Java 相同。无论如何,下面的代码。
<form id="eForm">
<script>
function CheckForm()
{
var first=document.getElementsByTagName("input")[0].value;
var mid=document.getElementsByTagName("input")[1].value;
var last=document.getElementsByTagName("input")[2].value;
var email=document.getElementsByTagName("input")[3].value;
var date=document.getElementsByTagName("input")[4].value;
var time=document.getElementsByTagName("input")[5].value;
if((document.getElementsByTagName("input")[0].value !=="first" || document.getElementsByTagName("input")[0].value !=="") &&
(document.getElementsByTagName("input")[1].value !=="Initial" || document.getElementsByTagName("input")[1].value !=="") &&
(document.getElementsByTagName("input")[2].value !=="last" || document.getElementsByTagName("input")[2].value !=="") &&
(document.getElementsByTagName("input")[3].value !=="someone@example.com" || document.getElementsByTagName("input")[3].value !=="") &&
(document.getElementsByTagName("input")[4].value !=="xx/xx/xxxx" || document.getElementsByTagName("input")[4].value !=="") &&
(document.getElementsByTagName("input")[5].value !=="xx:xx" || document.getElementsByTagName("input")[5].value !==""))
{
document.getElementsByTagName("input")[0].value='fail';
}
else
{
alert("One or more fields were not filled in correctly. /n Please try again.");
document.getElementsByTagName("input")[0].value='works';
}
first.value='works';
}
</script>
First name: <input type="text" name="FirstName" id="FirstName" value="first"><br>
Middle Initial: <input type="text" name="MidName" id="MidName" value="Initial"><br>
Last name: <input type="text" name="LastName" id="LastName" value="last"><br>
E-Mail: <input type="text" name="Email" id="Email" value="someone@example.com"><br>
Date for request: <input type="date" name="Date" id="Date" value="xx/xx/xxxx"><br>
Time for request: <input type="time" name="Time" id="Time" value="xx:xx">
AM<input type="radio" name="AM/PM" id="AM" checked="true">
PM<input type="radio" name="AM/PM" id="PM" checked="false"><br>
<input onclick="CheckForm()" type="button" value="Submit">
<input onclick="CheckForm()" type="submit" value="Submit">
On Submit, if a feild is left not filled out, then do not go through and show a message saying that "One or more feilds are not filled out. You must fill them
out in order to submit this form."
</form>
事后看来(仍然是 20/20),我应该意识到这个表达对我的 or 陈述总是正确的,所以感谢 v2b 指出这一点。