【发布时间】:2017-08-02 00:25:37
【问题描述】:
我试图弄清楚为什么如果我输入空值,警报仍然会在不应该出现的时候出现。参数声明所有值都不能为空,否则函数应该结束。
有人有什么想法吗?谢谢!
@charset "UTF-8";
/* CSS Document */
body{
height:1000px;
width:100%;
background:#fff;
margin:0;
}
.divider{
width:100%;
height:auto;
background:#CCC;
display:block;
margin:10px;
}
h2{
font-size:16px;
display:block;
}
#confirm-paragraph{}
#global-variable-paragraph{}
#user-input{}
#expressions{}
#elephant-paragraph{}
#method-1{}
#method-2{}
#ml{}
#litres{}
#conditional-operator{}
#cast-1{}
#cast-2{}
<!-- Checklist: Obtain user input and store it in variables -->
<!-- Report variable text to the client window -->
<!-- Prompt -->
<section class="divider">
<h2>User Input Variables Example</h2>
<button onclick="nameFunction()">Click Me</button>
<p id="user-input">This text should change after clicking the button.</p>
<p style="color:red; font-weight:bold">NOT WORKING Version3!!!!!!!!</p>
<p>Should not alert if null values are present - null values are present by just clicking OK when entering nothing. All values should not be null in order for the alert to appear</p>
<script>
function nameFunction() {
var yourforename = prompt("What is your first name?");
var yoursurname = prompt("What is your last name?");
var yourage = prompt("What is your age?");
if (yourforename != null && yoursurname != null && yourage != null) {
alert("Hello " + yourforename + " " + yoursurname + ". You are " + yourage + " years old.");
}
}
</script>
</section>
【问题讨论】:
-
很确定如果你没有在
prompt中输入任何内容,它会返回一个空字符串而不是null。此外,您可以删除 CSS/HTML 代码并删除两种语言的标签,因为它们与您的问题无关。
标签: javascript html css