-1
<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 中有条件,以便如果名称、地址、zip 等变量返回 null,它是空白的或完全从警报中排除。我不知道从哪里开始。

4

1 回答 1

0

您可以首先使用 if 语句构建字符串,然后对其发出警报,或者使用三元运算符,它充当内联 if:

window.alert (
    (name ? "Your name is " + name + "\n" : '') + 
    (address ? "Your address is " + address + "\n" : '') // + etc.
);

这将起作用,除非您希望显示这些值,即使它们是''undefinednull、或。false0NaN

于 2013-05-07T02:05:37.237 回答