<script>
var name, address, city, state, zip, birthdate , social, response1, info;
//Function call - vitalinfo prompts user 7 times
function vitalinfo() {
name = window.prompt("Please enter your name", "Example: Jane Doe");
address = window.prompt("Please enter your address", "Example: 123 Main St");
city = window.prompt("Please enter your city","Example: Punxsutawney");
state = window.prompt("Please enter your state", "Example: Pennsylvania");
zip = window.prompt("Please enter your zip","Example: 12345");
birthdate = window.prompt("Please enter your birthdate","12-29-1987");
social = window.prompt("Please enter your social security number","123-45-6789");
//end prompts
vitalinfo= window.confirm("Do you want to review the information you entered?");
if (vitalinfo == true)
{
window.alert (
"Your name is " + name + "\n" +
"Your address is " + address + "\n" +
"Your city is " + city + "\n" +
"Your state is " + state + "\n" +
"Your zip is " + state + "\n" +
"Your birth date is " + birthdate + "\n" +
"Your social security number is " + social);
}
}
我想在我的js中有条件,这样如果name,address,zip等变量返回null,它就是空白或完全从警报中排除。我不知道从哪里开始。
答案 0 :(得分:0)
您可以先使用if语句构建字符串,然后提醒它,或使用ternary operator作为内联函数:{/ p>
window.alert (
(name ? "Your name is " + name + "\n" : '') +
(address ? "Your address is " + address + "\n" : '') // + etc.
);
这将有效,除非您希望显示值,即使它们是''
,undefined
,null
,false
,0
或{{ 1}}。