为什么这个表单代码不起作用?这似乎合乎逻辑

时间:2017-08-19 00:06:01

标签: forms if-statement alert prompt statements

我试图编写一个简单的表单,要求输入以下内容:

  

情绪,年龄和性别

我在心情提示中放了什么并不重要。它总是积极的。对于这个年龄,如果他们未满18岁,我希望标签关闭。对于性别,它与情绪提示相同。任何帮助将不胜感激。



.showPhysicsShapes




2 个答案:

答案 0 :(得分:1)

我会让我的答案详细,以帮助你学习。对于初学者,当您要使用=甚至==时,您正在使用===。这就是为什么心情总是注册得好,因为userMood =" good"将userMood设置为“好”'所以表达式求值为" good",它验证if语句,因为字符串的FALSE是""并且每隔一个字符串都是true。 userMood = "good" || "Good"也不会按照您的想法行事。你需要检查两个字符串,如(userMood === "good" || userMood === "Good")

答案 1 :(得分:0)

我尝试将代码中的某些行改为以下内容。



var userMood = prompt("Hey! How's it going?");
    if (userMood == "good" || userMood == "Good") 
	    alert("Awesome! We just need you to fill out a form for security reasons.");
    else  
	    alert("Oh, I'm sorry to hear that... We just need you to fill out a form for security reasons.");


var userAge = prompt("What is your current age?");
    if (userAge >= 18 )
        {
	    alert("Great! You are the sufficient age to enter this webpage!");
	    userAge = true;
        }
    else if ( userAge < 18 && userAge > 0)
        {
	    alert("Sorry, you are too young to view this content...");
	    window.close();
        }
    else if ( userAge < 0 )
        {
        alert(" ***** prompt message for inputting negative number ****");     
        window.close();
        }

var userGender = prompt ("What is your gender?");
    if (userGender == "male" ||  userGender == "Male" || userGender == "female" || userGender == "Female")
        {
	    alert("Great! You're human!");
	    userGender = true;
         }   
    else 
        {
	    alert("The webpage has perdicted non-human cyber activity, you can no longer continue.");
	    window.close();
        }
&#13;
&#13;
&#13;