在开发人员工具中检测控制台输入

时间:2018-09-17 21:16:04

标签: javascript input console

我使用JavaScript已有一段时间了,我想知道如何检测和使用控制台输入,并将所述输入分配给变量。我一直在使用提示来获取用户输入,但是它效率低下并停止了该程序的其余部分,当我考虑到这种可能性时,我一直在寻找更好的选择。

1 个答案:

答案 0 :(得分:0)

Based on the comments I'd suggest something like this:

<form action="/process-form" method="post" id="myForm">
  <label for="myVar">What variable do you want to set?</label>
  <input name="myVar" id="myVar" value="">
  <button>Submit</button>
</form>

Process that with JS

var form = document.getElementById("myForm");
form.addEventListener("submit", function (event) {
  event.preventDefault();
  var formData = new FormData(event.target);
  // Assign the user input to the global scope so you can do whatever you want
  window.myVar = formData.get('myVar');
});