简体   繁体   English

如何检查js上的输入字段并根据文本值编写和回答

[英]How can I check the input field on js and write and answer based on the text value

im new to Javascript and still learning so sorry for the noob question.我是 Javascript 的新手,仍在学习,对于菜鸟问题​​很抱歉。 I want to write a chatbot but I am stuck at the very basic beginning.我想写一个聊天机器人,但我被困在最基础的开始。 This is what I wrote so far:这是我到目前为止所写的:


var btt = document.getElementsByClassName(bt);
        var a = document.getElementById("A").textObject.value;
        var b = document.getElementById("B").textObject.value;
        
        var msg = ["Hi", "Hey", "Hello"]

        function one() {
            if (a.value = "Hi") {
             document.getElementById("B").value = "Hello";
          }
        };

I have two input fields and a button.我有两个输入字段和一个按钮。 I am trying to get it to check the text value of the first input field (a) by pressing the button (btt) and replying with an answer text (b) based on that text input.我试图通过按下按钮 (btt) 并根据该文本输入回复答案文本 (b) 来检查第一个输入字段 (a) 的文本值。 I just can't figure out why it does'nt work.我只是不明白为什么它不起作用。

And this is what the html body looks like:这就是 html 正文的样子:


<div align="center" id="A">
            <input type="text" width="250px" height="150"
            placeholder="..." autocomplete="off">
        </div>
        <br>
          <button class="bt" onclick="one()" value="msg">Click me</button>
        <br>
        <br>
        <div align="center" id="B">
          <input type="text" width="250px" height="150"
            placeholder="..." autocomplete="off">
        </div>

You have defined the ids on the wrong element, it should be on input fields.您在错误的元素上定义了 id,它应该在输入字段上。

Few mistakes I have noticed in your code.我在您的代码中注意到了一些错误。

  1. Ids are defined on the div elements, it should be in input Id 定义在 div 元素上,它应该在输入中
  2. document.getElementsByClassName(bt) bt is a string and it should be wrap with quotes. document.getElementsByClassName(bt) bt是一个字符串,应该用引号括起来。
  3. document.getElementById("A") returns the input object, you don't need to add textObject.value document.getElementById("A")返回输入对象,不需要添加textObject.value
  4. In the if condition you are using single = it should be double == for value comparison or triple === for value and type comparison.在 if 条件中,您使用 single =它应该是 double ==进行值比较或三重===进行值和类型比较。

Try this.尝试这个。

 var btt = document.getElementsByClassName("bt"); var a = document.getElementById("A"); var b = document.getElementById("B"); var msg = ["Hi", "Hey", "Hello"] function one() { if (a.value === "Hi") { document.getElementById("B").value = "Hello"; } };
 <div align="center"> <input type="text" id="A" width="250px" height="150" placeholder="..." autocomplete="off"> </div> <br> <button class="bt" onclick="one()" value="msg">Click me</button> <br> <br> <div align="center"> <input type="text" id="B" width="250px" height="150" placeholder="..." autocomplete="off"> </div>

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

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