2

应该是这个需要的一个非常简单的修复......

我有一个注册表单,我想验证空字段并让 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,如果这有助于阐明预期的过程。

4

3 回答 3

2

d只有当等于noANDe为空时,警报才会显示。这同样适用于fg。这没有意义。举个例子:

我玩volleyball但不在校园里玩,所以我输入nof你的逻辑期望我在g你真正希望用户输入排球位置的地方放一个空字符串。

这应该改为:

else if (f == "no" && g != "") {

}  

请注意从==!=this 的变化意味着g不等于空字符串。和(如果设置为“否”,您不想为空)d也是e如此ed

验证您的密码客户端也是有风险的,我希望您有服务器端验证。

于 2013-08-16T18:48:04.617 回答
0

尝试这个:

else if (registration_form.field[1].checked == true && e == "") {
  alert("Please make sure to enter where you will play football.");
  return false;
  }

并使该单选按钮成为任何活动按钮

<input type="radio" name="field" checked="checked" value="yes">
<input type="radio" name="field" value="no">
于 2013-08-16T21:32:56.227 回答
0

如果后面的条件没有被评估,可能是因为前面的条件之一已经被评估并满足,并被返回。如果您想确认所有条件都在被评估,您可以删除 return 语句并向每个条件添加一个 console.log() 消息,输出条件的真/假状态,

于 2013-08-16T18:52:49.613 回答