【问题标题】:Simulated chatbot that switches input methods?切换输入法的模拟聊天机器人?
【发布时间】:2017-06-07 05:03:21
【问题描述】:

我创建了一个聊天机器人,它询问用户的用户名,然后将其存储为变量。但是,在我的一个对话框树中,我必须要求用户输入另一个答案,我让它工作,以便用户可以通过按钮点击进行导航。

在用户名捕获时,“输入栏”会从控件中移除,并且我会在每个消息功能中附加按钮。我试图做与此相反的操作(删除按钮并添加一个输入),它确实有效,但是我无法让我的输入做任何事情。它不会在输入时提交,它不会保存变量,在我的反复试验中,我发现如果我尝试设置一个 var 关闭那个 msg 值,它会更新用户名。

这是我检索名称的函数

function get_username() {
  send_msg("Hello I'm Mumbles, the Help Bot", function(){
    send_msg("Who am I speaking with today?")
  });
}

function ai(msg) {
  if (username.length < 3) {
    username = msg;
          send_msg("Nice to meet you"+username+". What are you working on today?" , function() {
            $("#controls").append(
              '<button class="options one" value="my_certifications" type="button">My Certifications</button>' +
                '<button class="options one" value="videos"type="button">Videos</butto/>' +
                '<button class="options one" value="login"type="button">Login</butto/>' +
                '<button class="options one" value="other"type="button">Other</butto/>' 
            );
          });
        }
      // Remove text bar
      $("#controls").empty();

};

这是我尝试捕获电子邮件的功能

    } else if (b_val == "my_practicum") {
      send_msg("What is the email address you submitted your practicum with? By having this we can give you a status update!" , function() {
            $("#controls").append(
                      '<textarea id="message" class="practicum-bar" placeholder="Type Your Name - Then Hit Enter"></textarea><button style="display:none;" id="sendMsg">Send</button>'                  );
                 email = msg;
        console.log(email);
      });
    } 
}

如果你想看看这个,我的JSFiddle你可以通过这个用户路径看到它

输入姓名 > 认证 > 我的实习 > 输入电子邮件

对此的任何帮助表示赞赏!谢谢!

【问题讨论】:

  • 输入邮箱后你想做什么
  • @jackblank 它会在运行 api 函数时询问后续问题(尚未放入示例中)。我只需要能够将其存储为变量

标签: javascript jquery html


【解决方案1】:

看起来您通过执行$("#controls").empty(); 删除了controls,并且您希望将事件添加到动态创建的元素,因此您必须将上下文添加到.on 的第二个参数。现在看you can see the console.log in the fiddle第二次点击回车,因为jquery现在可以检测到该项目。

$("#controls").on("keypress", "#message", function(event){

 if (event.which == 13) {
      console.log("my test");
      if ($("#slideThree").prop("checked")) {
         $("#sendMsg").click();
         event.preventDefault();
         //*edit you can now change variables outside of scope*
      }
  } 
});

现在您可以进行事件处理了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多