JavaScript confirm() 框从两个输入中获取值

JavaScript confirm() boxes get value from both inputs

我想知道是否有办法从 JavaScript 中的两个 confirm() 框获取输入值。 首先弹出 confirm() 框并询问 "OK" 或 "Cancel" 然后它再次弹出询问其他内容,再次 "Ok" 或 "Canel" 我想得到来自 confirm() 框 1 和 confirm() 框 2 的值,然后使用 ID "result".

写入 <p>

例如,如果用户在问题 "Is is warm?" 上单击 "OK",然后在下一个确认()框上单击 "OK"。我想写:天暖和下雨了

这是我尝试过的方法,但它只给了我第一个 confirm() 框的值。

function weather() {
    var sunny,rainy;
    var sunny= confirm("Is it warm?");
    var rainy= confirm("Is it raining?");

    if (sunny== true){
        ans = "Summer!";

        if (rainy == true) {
            ans = "It's sunny and it's raining.";
        }
    } else if(sunny == false){
        ans = "It's not summer then.";

        if (rainy== false){
            ans = "At least it's not raining!";
        }
    }

    document.getElementById("result").innerHTML = ans;
}

你可以使用条件运算符&&来更具体而不是嵌套,这样也会更清晰。

仅在两个选项之间做出决定时,您还可以使用三元运算符 (condition) ? true : false

下面我在字符串的连接中使用了它。

let a,b, str = '';
a = confirm("It is sunny.");
b = confirm("It is raining.");

if(a && b) str = "It's sunny and raining";
else if(!a && b || !b && a) str = "It's " + ((a) ? "sunny": "raining.");
else if(!a && !b) str = "Weather does not exist";

document.getElementById("result").innerHTML = str;
<div id="result"></div>

没有 else if 这样的东西,只有一个 else 块包含另一个 if

这个:

if (sunny == true) {
    // sunny is true
} else if(sunny == false) {
    // ...
}

实际意思是:

if (sunny == true){
    // sunny is true (actually "truthy": https://developer.mozilla.org/en-US/docs/Glossary/Truthy)
} else {
    // sunny is NOT true, so sunny is definitely false (actually "falsy": https://developer.mozilla.org/en-US/docs/Glossary/Falsy)

    if (sunny == false) {
        // no need to test that, we already know that sunny is false
    }
}

关于您的其余代码,这里有一些提示:

function weather() {
  var sunny, rainy; // No need to declare those variables here since you redeclare on the next lines.
  var sunny = confirm("Is it warm?");
  var rainy = confirm("Is it raining?");
  if (sunny == true) {
    ans = "Summer!"; // missing var keyword here
    if (rainy == true) { // confirm returns a boolean, you should check for strict equality with ===
      ans = "It's sunny and it's raining.";
    }
    // You are not handling the case where sunny is true and rainy is false
  } else if (sunny == false) {
    ans = "It's not summer then.";
    if (rainy == false) { // confirm returns a boolean, you should check for strict equality with ===
      ans = "At least it's not raining!";
    }
    // You are not handling the case where sunny is false and rainy is true
  }
  document.getElementById("result").innerHTML = ans;
}

最后,你可能只需要 += (concatenation)

function weather() {
  var sunny = confirm("Is it warm?");
  var rainy = confirm("Is it raining?");
  var ans;

  if (sunny === true) {
    ans = "Summer!";

    if (rainy === true) {
      ans += "It's sunny and it's raining.";
    }
  } else {
    ans = "It's not summer then.";

    if (rainy === false) {
      ans = "At least it's not raining!";
    }
  }

  document.getElementById("result").innerHTML = ans;
}

weather();
<p id="result"></p>

感谢大家的帮助!所有的答案都让我想到了什么! :) 我是 JavaScript 的初学者,很抱歉我的编码不好...

我编辑了其中一个答案并这样做以涵盖所有不同的四种情况:

  function weather() {
    a = confirm("Is it sunny?");
    b = confirm("Is it raining?");
    
    if(a && b) str = "It's sunny and raining.";
    else if(!a && b) str = "It's not sunny and it's raining.";
    else if(a && !b) str = "It's sunny and it's not raining.";
    else if(!a && !b) str = "It's not sunny and it's not raining."; 
    document.getElementById("result").innerHTML = str;
    }
<button onClick="weather()">Test me!</button>
<p id="result"></p>