html按钮javascript功能

时间:2017-03-20 13:50:23

标签: javascript html

我想要一个按钮在我的javascript文件中执行一个函数,但是当单击该按钮时它无法执行此操作。



function output() {
  if (form.inl.value == "1" && form.gate.value == "and" && form.inll.value == "0") {
    alert("output is 0")
  } else {
    if (form.inl.value == "1" && form.gate.value == "and" && form.inll.value == "1") {
      alert("output is 1")
    }
  }
}

<select id="inl">
  <option value="1">1</option>
  <option value="0">0</option>
</select>
<select id="gate">
  <option value="and">and</option>
  <option value="or">or</option>
  <option value="not">not</option>
</select>
<select id="inll">
  <option value="1">1</option>
  <option value="0">0</option>
</select>
<input id="out" type="button" value="output" onclick="output();">
&#13;
&#13;
&#13;

我做错了什么?

5 个答案:

答案 0 :(得分:0)

尝试以下方法:

&#13;
&#13;
function output(){
  var inl = document.getElementById('inl').value;
  var ln1 = document.getElementById('inll').value;
  var gate = document.getElementById('gate').value;
  if((inl == "1" && gate == "or" && ln1 == "0") || (inl == "0" && gate == "or" && ln1 == "1") || (inl == "1" && gate == "and" && ln1 == "1") || (inl == "1" && gate == "or" && ln1 == "1")){
      alert("output is 1")
  }
  else{
    alert("output is 0")
  }
}
&#13;
<select id="inl">
  <option value="1">1</option>
  <option value="0">0</option>
</select>
<select id="gate">
  <option value="and">and</option>
  <option value="or">or</option>
  <option value="not">not</option>
</select>
<select id="inll">
  <option value="1">1</option>
  <option value="0">0</option>
</select>
<input id="out" type="button" value="output" onclick="output();"></body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

现在它有效,你试图从你代码中没有的元素中获取价值。

JQuery方法:

&#13;
&#13;
function output(){
if($("#inl")[0].value == "1" && $("#gate")[0].value == "and" && $("#inll")[0].value == "0"){
    alert("output is 0")

}
else{
if($("#inl")[0].value == "1" && $("#gate")[0].value == "and" && $("#inll")[0].value == "1"){
        alert("output is 1")
    }
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="inl">
        <option value="1">1</option>
        <option value="0">0</option>
    </select>
    <select id="gate">
        <option value="and">and</option>
        <option value="or">or</option>
        <option value="not">not</option>
    </select>
    <select id="inll">
        <option value="1">1</option>
        <option value="0">0</option>
    </select>
    <input id="out" type="button" value="output" onclick="output();"></body>
&#13;
&#13;
&#13;

Javascript方法:

&#13;
&#13;
function output(){
if(document.getElementById("inl").value == "1" && document.getElementById("gate").value == "and" && document.getElementById("inll").value == "0"){
    alert("output is 0")

}
else{
if(document.getElementById("inl").value == "1" && document.getElementById("gate").value == "and" && document.getElementById("inll").value == "1"){
        alert("output is 1")
    }
}
}
&#13;
<select id="inl">
        <option value="1">1</option>
        <option value="0">0</option>
    </select>
    <select id="gate">
        <option value="and">and</option>
        <option value="or">or</option>
        <option value="not">not</option>
    </select>
    <select id="inll">
        <option value="1">1</option>
        <option value="0">0</option>
    </select>
    <input id="out" type="button" value="output" onclick="output();"></body>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

Try this
function output(){
if(form.elements["inl"].value == "1" && form.elements["gate"].value == "and" && form.elements["inll"].value == "0"){
    alert("output is 0")

}
else{
if(form.elements["inll"].value == "1" && form.elements["gate"].value == "and" && form.elements["inll"].value == "1"){
        alert("output is 1")
    }
}

答案 3 :(得分:0)

您可以按名称访问表单。我会按名称抓取字段并将它们解析为整数。

然后执行原生计算。

Properties.Settings.Default["Version"] = File.GetLastWriteTime(mainDllPath).ToString("ddMMyyyyhhmmss");
Properties.Settings.Default.Save();
function output() {
  let form  = document.forms['my-logic-form'],
      left  = parseInt(form.inl.value, 10),
      op    = form.gate.value,
      right = parseInt(form.inll.value, 10);
  
  alert('Output is: ' + doCalculation(op, left, right));
}

function doCalculation(op, left, right) {
  switch (op) {
    case 'and' : return left && right;
    case 'or'  : return left || right;
    case 'not' : return !left;         // Unary (right is ignored) 
    default    : return -1;            // Undefined operation
  }
}

答案 4 :(得分:0)

由于浏览器可能会出现此问题:

解决方案是:

1:重命名&#34;输出()&#34;功能到任何类似&#34; output1()&#34;

2:替换onclick =&#34;输出();&#34; to onclick =&#34; return output();&#34;

3:你可以使用Debugger;并检查您的功能是否被调用。    它就像检查点,所以你可以按ctrl + shift + i然后按F10逐行执行代码来检查你的代码流程。

例如

function output() {
    debugger;
  if (form.inl.value == "1" && form.gate.value == "and" && form.inll.value == "0") {
    debugger;
    alert("output is 0")
  } else {
    debugger;
    if (form.inl.value == "1" && form.gate.value == "and" && form.inll.value == "1") {
      alert("output is 1")
    }
  }
}

这个解决方案中的一个绝对对您有用。 谢谢:)