2017-03-17 66 views
-2

我有兩個按鈕,名爲sqr1和sqr2,我想要的是當兩個按鈕被點擊時.ie如果IF條件滿足,它應該提醒消息,但即使任何一個按鈕點擊。當兩個按鈕被點擊時我都想要它。在javascript中使用IF語句比較變量

new1.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> 

new2.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> 
+2

您正在比較的變量與單擊任何內容完全無關。 – Quentin

+1

sqr1如何設置? sqr2如何設置?比較中所有變量都有哪些值?你使用調試器時發現了什麼?你需要向我們展示除「我寫這個並且它不起作用」之外的某種努力。 – csmckelvey

+0

我在sqval1和另一個sqval2中有一些價值,而當sqr1具有sqval1的值而sqr2具有sqval2的值時,sqr1和sqr2是按鈕的名稱,它應該給出消息。 – vamshi

回答

0

我不知道這是否是你想要做什麼,但是這將顯示一個警告只有兩個按鈕被壓:

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; 
    } 
}