简体   繁体   English

document.getElementById().value 没有返回正确的值

[英]document.getElementById().value doesn't return the right value

I'm trying to create an input field on the tablet of Pepper robot which allows user to input their email address by voice.我正在尝试在 Pepper 机器人的平板电脑上创建一个输入字段,允许用户通过语音输入他们的 email 地址。 I use a speech recognition box to get the letter that user says and raise an event so that Javascript can catch it and modify the text.我使用语音识别框来获取用户所说的字母并引发一个事件,以便 Javascript 可以捕获它并修改文本。 But it seems that "document.getElementById("abc").value" never get what is currently on the screen but the defaut value.但似乎 "document.getElementById("abc").value" 永远不会得到当前屏幕上的内容,而是默认值。

Example: I say "1", it shows "hello1" (Totally good).示例:我说“1”,它显示“hello1”(完全好)。 Then I say "2", it shows "hello2" (Expecting "hello12")然后我说“2”,它显示“hello2”(期待“hello12”)

Here is my JS code:这是我的 JS 代码:

var session = new QiSession(function(session) {
            // "Connection esterblished!";
          }, function() {
            // "Could not connect to the robot";
          });

// Subscribe to ALMemory Service   
session.service("ALMemory").then(function(ALMemory) {
  // "ALMemory proxy subscription successful!";  
  ALMemory.getData('voice_input').then(function(voice_input){  
        document.getElementById("abc").value += voice_input;        
  });

});

HTML code: HTML 代码:

<input type="text" name="email" id="abc" value="hello">

Is there anything to do with the "session"?与“会话”有什么关系吗? Because even if I use a global variable to store the voice inputs, I can only modify the value of this variable but cannot get the current value of this variable inside the fonction(what I can get is the initialized value).因为即使我使用一个全局变量来存储语音输入,我也只能修改这个变量的值,而不能在函数内部得到这个变量的当前值(我能得到的是初始化值)。

Unfortunately, I was unable to find proper public documentation on the JavaScript API.不幸的是,我无法在 JavaScript API 上找到适当的公共文档。 However, this is what I could find.但是,这是我能找到的。

It appears you are using promises as event listeners.看来您正在使用 Promise 作为事件侦听器。 Promises fire once, while event listeners fire repeatedly. Promise 触发一次,而事件侦听器重复触发。 You are using what they refer to as calls (promises) instead of signals (events).您正在使用他们所说的调用(承诺)而不是信号(事件)。 If you have documentation on signals, I would use them instead, because they more closely resemble what you are trying to do.如果您有关于信号的文档,我会改用它们,因为它们更类似于您正在尝试做的事情。 Otherwise, you could do something like this:否则,您可以执行以下操作:

var session = new QiSession(function(session) {
    // "Connection esterblished!";
    // Subscribe to ALMemory Service
    session.service("ALMemory").then(function(ALMemory) {
        // "ALMemory proxy subscription successful!";
        function getVoice() {
            ALMemory.getData('voice_input').then(function(voice_input){
                document.getElementById("abc").value += voice_input;
                getVoice();
            });
        }
        getVoice();
    });
}, function() {
    // "Could not connect to the robot";
});

First, I moved the service subscriber into the callback for QiSession's successful connection, because that is usually where session would be defined in an async function call.首先,我将服务订阅者移动到 QiSession 成功连接的回调中,因为这通常是在异步 function 调用中定义 session 的地方。 When ALMemory has been subscribed to, it defines and calls getVoice, which retrieves the user's speech, appends to the field, and then runs getVoice again to queue another listen.当 ALMemory 被订阅时,它定义并调用 getVoice,它检索用户的语音,附加到字段,然后再次运行 getVoice 以排队另一个听。 You may want to add a conditional to stop it eventually.您可能希望添加一个条件来最终停止它。 It may also be worth looking into async/await if Pepper supports this, because then you can do this easily with a while loop.如果 Pepper 支持这一点,也可能值得研究 async/await,因为这样你就可以使用 while 循环轻松地做到这一点。

There is nothing wrong with document.getElementById() . document.getElementById()没有任何问题。 The problem is that you must react to voice input, and not only get the current voice input.问题是您必须对语音输入做出反应,而不仅仅是获取当前的语音输入。 This is done by subscribing to the ALMemory event .这是通过订阅 ALMemory 事件来完成的。 Here is the tutorial's snippet roughly adapted to your case (but not tested):这是大致适合您的案例的教程片段(但未经测试):

session.service("ALMemory").then(function (ALMemory) {
  ALMemory.subscriber("voice_input").then(function (subscriber) {
    subscriber.signal.connect(function (state) {
      document.getElementById("abc").value += voice_input;
    });
  });
});

Please note that to work, you must publish an event on the "voice_input" key, using ALMemory.raiseEvent .请注意,要工作,您必须使用ALMemory.raiseEvent"voice_input"键上发布事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM