2013-07-20 194 views
-2

在Js中,我如何比較兩個值。我有三個按鈕,每個按鈕都會生成13之間的數字。JavaScript比較兩個值

我想比較例如第一個按鈕可以1,第二2,第三3他們與button的數量輸出的數量。

如果該按鈕生成的數字與2相同,則表示它爲平局。

function Random() 
{ 
    var result = display.innerHTML = Math.floor((Math.random() * 3) + 1); 
} 

HTML代碼:

<form> 
    <input id="rock"  type="button" onclick="Random()" value="Rock"  /> 
    <input id="paper" type="button" onclick="Random()" value="Paper" /> 
    <input id="scissors" type="button" onclick="Random()" value="Scissors" /> 
</form> 
+2

也許可以使用''''''=='運算符。 – Thilo

+0

你的'按鈕'代碼是什麼?請將它們放在這裏 –

+0

回答

0

==是比較運算符

function Random() 
{ 
    var result = display.innerHTML == Math.floor((Math.random() * 3) + 1); 
} 
2

===用於爲嚴格的比較操作

var display = document.getElementById("respective selector ID"); 
var result = parseInt(display.innerHTML) === Math.floor((Math.random() * 3) + 1); 

更新:

既然你更新你的問題,在這裏

HTML:

<form> 
     <input id="rock" type="button" onclick="Random(this.id)" value="1" /> 
     <input id="paper" type="button" onclick="Random(this.id)" value="2" /> 
     <input id="scissors" type="button" onclick="Random(this.id)" value="3" /> 
    </form> 
    <span id="show"></span> 

JS:

function Random(id) { 
    var display= parseInt(document.getElementById(id).value); 
    if(display === Math.floor((Math.random() * 3) + 1)){ 
     document.getElementById('show').innerHTML = "EQUAL"; 
    } 
    else{ 
      document.getElementById('show').innerHTML = "NOT EQUAL"; 
    } 
} 

JSFiddle

(1)的innerHTML /值總是返回數據類型string,使用這種我傳遞按鈕的ID,以便I'hv使用.parseInt()
(2)onclick="Random(this.id)"

希望你能理解。

1

您可以與=====進行比較。

請閱讀有關JavaScript的比較operators

,懂=====它們被用於不同的必要性和重要性。

var x = 5, y = 10; 
x === y //false 
x === x //true 
x == "5" //true 
x === "5" //false 
x < y //true 
x > y // false 
0

我從以前的問題複製就同一問題

<input id="rock" type="button" onclick="Random(0)" value="Rock"/> 
<input id="paper" type="button" onclick="Random(1)" value="Paper"/> 
<input id="scissors" type="button" onclick="Random(2)" value="Scissors"/> 
<br /> 
<span id="display"></span> 

那麼一些部分

function Random(choice) { 
    var system = Math.floor(Math.random() * 3); 
    display.innerHTML = system == choice ? 'draw' : system; 
} 

演示:Fiddle

+0

var system = Math.floor(Math.random()* 3);, –

+0

請問您可以解釋一下這個代碼display.innerHTML = system == choice? 'draw':system;什麼?和:做? –

+0

@WerErew它被稱爲**三元運算符(條件?if block:else block)**。曾經是內聯* if .. else .. *語句。 – Praveen

0

或者,如果你喜歡用jQuery工作,你可以使用這個:

<button id="button_1" onclick="myRandom('button_1')">1</button> 
<button id="button_2" onclick="myRandom('button_2')">2</button> 
<button id="button_3" onclick="myRandom('button_3')">3</button> 

而且在javascript:

function myRandom(id) { 
    a = Math.floor((Math.random() * 3) + 1); 
    if ($("#" + id).html() == a) 
     alert("Button " + $("#" + id).html() + " is Match. Generated value is " + a + "."); 
    else 
     alert("Button " + $("#" + id).html() + " is not Match. Generated value is " + a + "."); 
} 

Fiddle demo

0

通常您會使用==運算符,如果您想進行嚴格比較,請使用===

例如

if(a == b) 
{/*if true*/} 

上述情況,如果一個是string具有1該值和b爲int具有1值時,它將評估爲真。

,但如果你做到以下幾點:

if(a === b) 
{/*if true*/} 

A和B必須的string要麼int

在你的情況下,只需==將正常工作。