【问题标题】:Why is javascript not recognizing my input as a varaible?为什么 javascript 不能将我的输入识别为变量?
【发布时间】:2021-09-09 05:55:18
【问题描述】:

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

代码附在下面,演示网站链接在这里: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>

感谢您的帮助。

【问题讨论】:

  • 问题很简单。您在函数中声明 inputname,因此它仅在该函数中可用。将声明移到函数之外,使其可用于其他函数。
  • 这能回答你的问题吗? What is the scope of variables in JavaScript?

标签: javascript html input var


【解决方案1】:

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

var inputname

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

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

【讨论】:

  • 谢谢,我不知道!但是现在,innerHTML 正在打印“未定义”而不是输入的名称。对于另一个问题,我很抱歉,但有没有办法解决这个问题?
  • 你先设置你的名字?警报正常吗?我在答案中添加了一行以防止出现“未定义”.. 您确定您从 saveName 函数中删除了 var 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
相关资源
最近更新 更多