在 javascript 中使用 IF 语句比较变量



我有两个名为sqr1和sqr2的按钮,我想要的是当两个按钮都被单击时,即如果IF条件满足它应该发出警报消息,但即使单击任何一个按钮,它也会显示警报。当两个按钮都被单击时,我想要它。

新1.html

<script type="text/javascript"> 
function saveVal() {
    var squ1 = document.getElementById("sq1").value;
    localStorage.setItem("sq1", squ1);
    squ1 = localStorage.getItem("sq1");
    var squ2 = document.getElementById("sq2").value;
    localStorage.setItem("sq2", squ2);
    squ2 = localStorage.getItem("sq2");
</script>
<html>
<body>
<form method="get" action="new2.html">
<input type="number" id="sq1" name="sqrone"   placeholder=" Enter Number" class="tictac"  />
<input type="number" id="sq2" name="sqrtwo"   placeholder=" Enter Number" class="tictac"  />
<input type="submit"  value="Submit" onclick="saveVal()">
</form>
</body>
</html>

新2.html

<script>
    var sqr1
    var sqr2
var square1 = document.getElementById("sq1");
    square1.value = localStorage.getItem("sq1");
    var square2 = document.getElementById("sq2");
    square2.value = localStorage.getItem("sq2");
    var sqval1 = document.write(square1.value);
    var sqval2 = document.write(square2.value);
    function check{
    if((sqr1 ==  sqval1)  && (sqr2 ==  sqval2)  ) 
      {
          alert('you win!')
      }
}
</script>
<html>
<body>
<INPUT TYPE="button" id="sq1" NAME="sqr1"  class="tictac"    onClick=" check() ">
<INPUT TYPE="button" id="sq2" NAME="sqr2"  class="tictac"   onClick="check() ">
</body>
</html>

我不确定这是否是您要做的,但仅当两个按钮都被按下时,这才会显示警报:

.HTML

<button id="button1" type="button" onclick="button1check()">button1</button>
<button id="button2" type="button" onclick="button2check()">button2</button>

.JS

button1clicked = 0;
button2clicked = 0;
function button1check() {
  button1clicked = 1;
  if (button1clicked == 1 && button2clicked == 1) {
    alert('you win!');
    button1clicked = 0;
    button2clicked = 0;
  }
}

function button2check() {
  button2clicked = 1;
  if (button1clicked == 1 && button2clicked == 1) {
    alert('you win!');
    button1clicked = 0;
    button2clicked = 0;
  }
}

相关内容

  • 没有找到相关文章

最新更新