如何使用JavaScript实现OR门和AND门操作?

时间:2016-02-11 01:32:56

标签: javascript html

我有一个问题是制作JavaScript代码和HTML代码来实现OR-gate和AND-gate的逻辑运算。首先,我需要用它的操作制作OR-gate对象和AND-gate对象(& #34; *"," +")。完整代码如下所示:

<html>
<head>
<script>
/*var OR_gate = {
    name: "or_gate";
    operation: '+';

};
var AND_gate = {
    name: "and_gate";
    operation: '*';

};*/

function getOutputResult()
{
    var input1 = document.getElementByName('input1').value;
    var input2 = document.getElementByName('input2').value;
    if(document.getElementByName('gate').name == "or_gate")
    {
        var logic = input1 + input2;
    }
    if(document.getElementByName('gate').name == "and_gate")
    {
        var logic = input1 * input2;
    }
    document.getElementByName("output").innerHTML = logic;
  }
  </script>
  </head>
  <body>
<h3><u> OR , AND gates operation </u></h3>
<form>

 Input 1: <select name="input1"><option value="1">1</option><option value="2">0</option></select><br><br>

 Input 2: <select name="input2"><option value="1">1</option><option value="2">0</option></select><br><br>

 Operation gate: <select name="gate"><option name="or_gate" >OR gate</option><option name="and_gate" >AND gate</option></select><br><br>

 <button onclick="getOutputResult()">DONE</button>
 </form>
 <br><br>
 Result : <label name="output">OUTPUT</label>

 </body>  
 </html>

1 个答案:

答案 0 :(得分:1)

instance XMonad XMaybe 'False b where
  type XResultT XMaybe 'False b = 'False
  (>>~) _ _ = XNothing

instance XMonad XMaybe 'True 'False where
  type XResultT XMaybe 'True 'False = 'False
  (>>~) _ _ = XNothing

instance XMonad XMaybe 'True 'True where
  type XResultT XMaybe 'True 'True = 'True
  (>>~) (XJust x) f = f x

在JavaScript中,您没有明确声明字符串的类型。此外,字符串是原始的。访问字符串的string logic = input1+operation+input2; document.getElementByName("output").innerHTML = logic.value; 属性的默认结果是value

可能还有其他问题,但您肯定需要将上面的代码更改为:

undefined

此外,

var logic = input1 + operation + input2;
document.getElementByName("output").innerHTML = logic;

对任何事情都无效。您似乎想要传递getOutputResult(this(input1),this(input2),this(gate)) input1,但无法像input2那样访问它们。你需要从DOM中获取它。最简单的方法可能就是给每个字段一个唯一的ID,以便您可以在全球范围内执行类似this(varname)

的操作