使用JavaScript提示输入并传递它

时间:2016-12-07 05:33:39

标签: javascript html

好吧..我有一个大脑放屁。也许我今天工作很久,需要离开。无论如何,我想要做的是获取二进制输入,使用javascript来提示用户将输入所需键的提示。然后他们的答案将被传递给输入。

    <!Doctype html>
<html>
<h1> Java input on Option change </h1>

<body>
<table>
    <tr>
        <td>option</td>
        <td>
<select id="keyselect" onchange="keyPrompt()" >
    <option value="0">No</option>
    <option value="1">Yes</option>
</select>
</td>
</tr>
</table>

<input name="keys" id="keysneeded" hidden>
<p id="keysneeded"> </p>
</body>
<script>
function keyPrompt()
{
    var k = document.getElementByID("keyselect").value;
        if (k != 1);{
            var keys = prompt("What keys are needed?");
            document.getElementByID("keysneeded").innerHTML = keys;
        }

    }


</script>
</html>

1 个答案:

答案 0 :(得分:3)

正如@teemu在评论getElementById而不是getElementByID中所说,并为每个控件使用不同的ID。并且在if条件之后删除;

function keyPrompt() {
  var k = document.getElementById("keyselect").value;
  if (k != 1) {
    var keys = prompt("What keys are needed?");
    document.getElementById("keysneeded").innerText = keys;
    document.getElementById("hidInput").value = keys;
  }

}
<!Doctype html>
<html>
<h1> Java input on Option change </h1>

<body>
  <table>
    <tr>
      <td>option</td>
      <td>
        <select id="keyselect" onchange="keyPrompt()">
          <option value="0">No</option>
          <option value="1">Yes</option>
        </select>
      </td>
    </tr>
  </table>

  <input name="keys" id="hidInput" hidden>
  <p id="keysneeded"></p>
</body>

</html>