如果两个按钮的功能具有不同的值

时间:2018-04-07 17:53:01

标签: javascript function if-statement button

我需要带有if功能的两个按钮

1)满足第一个条件:

  

如果输入数字2 - 是,如果数字< 2且数字> 2 -INCORRECTLY。

2)第二个条件必须是:

  

如果输入数字3 - 是,如果数字<3,如果输入数字&gt; 3 -INCORRECTLY。

第二个条件未得到满足。我怎样才能满足这两个条件?

&#13;
&#13;
<!DOCTYPE html>
<html>
<body>
<p>ENTER A NUMBER:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">VERIFY</button>
<p id="message"></p>
<script>
function myFunction() {
    var message, x;
    message = document.getElementById("message");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
	    try { 
        if(x == "2")  throw "TRUE !"  ;
		if(isNaN(x)) throw "not a number";
        x = Number(x);
		if(x > 2)    throw "INCORRECTLY";
		if(x < 2)   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ;    }}
</script>
</body>
</html>
<body>
<p>ENTER A NUMBER:</p>
<input id="demo" type="text">
<button type="buttonA" onclick="myFunctionA()">VERIFY</button>
<p id="message"></p>
<script>
function myFunctionA() {
    var message, x;
    message = document.getElementById("message");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
	    try { 
        if(x == "3")  throw "TRUE !"  ;
		if(isNaN(x)) throw "not a number";
        x = Number(x);
		if(x > 3)    throw "INCORRECTLY";
		if(x < 3)   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ;    }}

</script>
</body>
</table>
</html>
&#13;
&#13;
&#13;

enter image description here

1 个答案:

答案 0 :(得分:0)

我自己做了。

<!DOCTYPE html>
<html>
<body>
<p>ENTER A NUMBER:</p>

<input id="demo1" type="text">
<button type="buttonA" onclick="myFunction()">VERIFY</button>
<p id="message1"></p>

<script>
function myFunction() {
    var message, y;
    message = document.getElementById("message1");
    message.innerHTML = "";
    y = document.getElementById("demo1").value;
	
		try { 
        if( y == "2" )  throw "TRUE !"  ;
		if(isNaN(y)) throw "not a number";
        x = Number(y);
		if(y > 2 )    throw "INCORRECTLY";
		if(y < 2 )   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ; 	
		}}
		
</script>

</body>
</html>
<body>
<p>ENTER A NUMBER:</p>

<input id="demo2" type="text">
<button type="buttonB" onclick="myFunctionB()">VERIFY</button>
<p id="message2"></p>

<script>
function myFunctionB() {
    var message, y;
    message = document.getElementById("message2");
    message.innerHTML = "";
    y = document.getElementById("demo2").value;
	
		try { 
        if( y == "3" )  throw "TRUE !"  ;
		if(isNaN(y)) throw "not a number";
        y = Number(y);
		if(y > 3 )    throw "INCORRECTLY";
		if(y < 3 )   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ;
 		}}
		
</script>
</body>
</table>
</html>

`