0

我想检测何时有人没有在值输入中输入任何内容并点击提交。当我将大小写设置为仅两个引号时,它不起作用(显示的默认大小写:“lol”)。如果我使用两个引号之间有一个空格,那么我必须在输入中输入一个空格,然后点击提交才能看到“请输入您的 RSVP 代码”。

function myFunction(){

        var pincode = document.getElementById("pincode").value;

        switch (pincode){

            case "77642" :
                window.location.assign("http://wherever.com");
                break;

            case "" :
                document.getElementById("error").innerHTML = "Please enter your RSVP code.";
                break;

            default :
                document.getElementById("error").innerHTML = "lol";
                break;

            }
        }

似乎我错过了一件简单的事情,但我一直无法找到答案。谢谢!

4

1 回答 1

0

在 switch 之前添加 if 语句以验证输入:

function myFunction(){

    var pincode = document.getElementById("pincode").value;

    if(pincode.trim() == "") { // note the trim for eliminating empty spaces
        document.getElementById("error").innerHTML = "Please enter your RSVP code.";
        return;
    }

    switch(pincode) {
        ...
    }
}
于 2013-08-20T18:37:45.827 回答