为什么 javascript 没有将我的输入识别为变量?

Why is javascript not recognizing my input as a varaible?

我正在处理这段代码,当你通过设置按钮输入你的名字时,代码应该将你的名字保存在变量“inputname”中,所以当你对程序说“你好”时,该程序应该输出“你好”+你输入的名字,但由于某种原因它不会工作。这是为什么?

代码附在下面,演示网站链接在这里:https://javascript-test-3.stcollier.repl.co/

function record() {
  var recognition = new webkitSpeechRecognition();
  recognition.lang = "en-GB";
  recognition.start();
  recognition.continuous = true;
  recognition.onresult = function(event) {
    let transcript = event.results[0][0].transcript;
    var str = transcript;
    let msg_hello = ['Hello ' + inputname, 'Hello!', 'Hey ' + inputname];
    if (str.includes('hello')) {
      document.getElementById("output").innerHTML = (msg_hello[Math.floor(Math.random() * msg_hello.length)]);
      responsiveVoice.speak(msg_hello[Math.floor(Math.random() * msg_hello.length)]);
    } else {
      document.getElementById('output').innerHTML = "I don't know what you mean."
      responsiveVoice.speak(msg_notunderstood[Math.floor(Math.random() * msg_notunderstood.length)]);
    }
    document.getElementById('speechToText').value = event.results[0][0].transcript;
  }
}

//Mic Trigger Key
document.body.onkeyup = function(e) {
  if (e.keyCode == 32) {
    record()
  }
}
//Modal
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
  modal.style.display = "block";
}
span.onclick = function() {
  modal.style.display = "none";
}
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

//Input
function saveName() {
    var inputname = document.getElementById('savedName').value;
    alert("You entered your name as " + inputname)
    return false;
}
#output {
  text-align: center;
  font-family: 'Times New Roman', Times, serif;
  font-size: 20px;
}
/* Modal Stuff */
/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <link rel="stylesheet" href="style.css">
   <title>Document</title>
</head>
<body>
   <label for="Speech Recognition">Speech Recognition</label>
   <input type="text" name="" id="speechToText" placeholder="Speak Something" disabled="disabled">
   <button onclick="record()">Record</button>
   <p id="output"></p>
   <button id="myBtn">Settings</button>
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <input placeholder="Enter your name" type="text" size="12" id="savedName" />
    <button onclick="return saveName();">Save</button><span title="We use your name for making your experience with Argon more personal." style="cursor:help;"> &#63;</span>
   <script src="https://code.responsivevoice.org/responsivevoice.js?key=x9uXdCB8"></script>
   <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
   <script src="script.js"></script>
</body>
</html>

感谢您的帮助。

当您在一个函数内定义一个变量(使用 var)时,该变量被限制为 仅该函数 。在函数之外定义 inputname,以便其他函数可以访问它

var inputname

function record() {
  ....
  if (!inputname) inputname = 'You'; // default
  let msg_hello = ['Hello ' + inputname,  ....
  ....
}

function saveName() {
   inputname = document.getElementById('savedName').value;
   ...