应该是这个需要的一个非常简单的修复......
我有一个注册表单,我想验证空字段并让 javascript 在必要时返回一个简单的弹出警报。这是脚本代码:
<script type="text/javascript">
function validateForm() {
var a = document.forms["registration_form"]["school"].value;
var b = document.forms["registration_form"]["name"].value;
var c = document.forms["registration_form"]["password"].value;
var d = document.forms["registration_form"]["field"].value;
var e = document.forms["registration_form"]["field_location"].value;
var f = document.forms["registration_form"]["volleyball_campus"].value;
var g = document.forms["registration_form"]["volleyball_location"].value;
var h = document.forms["registration_form"]["indoor"].value;
var i = document.forms["registration_form"]["outdoor_courts"].value;
var j = document.forms["registration_form"]["blackout"].value;
var k = document.forms["registration_form"]["blackout_dates"].value;
if (a == "" || b == "") {
alert("Please make sure to enter your name and school.");
return false;
} else if (c != "pal") {
alert("Sorry, your password is invalid.");
return false;
} else if (d == "no" && e == "") {
alert("Please make sure to enter where you will play football.");
return false;
} else if (f == "no" && g == "") {
alert("Please make sure to enter where you will play volleyball.");
return false;
} else if (h == "no" && i == "0") {
alert("Please make sure to select how many outdoor volleyball courts you will use.");
return false;
} else if (j == "yes" && k == "") {
alert("Please make sure to enter your blackout dates.");
return false;
}
}
</script>
对于变量 a、b 和 c,一切正常。但是,以变量 d && e 开头的 else if 条件似乎不会执行。我想我的措辞一定有问题,但我无法弄清楚它是什么。
我已经设置了我的服务器端 php 脚本来做完全相同的事情,并且它可以正常工作。在这里你可以看到它:
//Validate the form:
if ($_POST['school'] == "" || $_POST['name'] == "") {
echo "Please make sure to enter your name and school.";
} elseif ($_POST['password'] != "pal") {
echo "Sorry, your password is invalid.";
} elseif ($_POST['field'] == "no" && $_POST['field_location'] == "") {
echo "Please make sure to enter where you will play football.";
} elseif ($_POST['volleyball_campus'] == "no" && $_POST['volleyball_location'] == "") {
echo "Please make sure to enter where you will play volleyball.";
} elseif ($_POST['indoor'] == "no" && $_POST['outdoor_courts'] == "0") {
echo "Please make sure to select how many outdoor volleyball courts you will use.";
} elseif ($_POST['blackout'] == "yes" && $_POST['blackout_dates'] == "") {
echo "Please make sure to enter your blackout dates.";
} else {
这可以按照我的意图捕获所有内容,但是客户端 Javascript 验证(我首先运行它只是为了更用户友好的外观)似乎无法通过前三个变量。
我无法为我的生活找出原因。我已经在互联网上进行了搜索,但找不到我正在寻找的答案。任何帮助将非常感激!
PS 该页面本身位于http://www.712jefferson.org/pal/registration.html,如果这有助于阐明预期的过程。