尽管在HTML文件中以脚本形式列出了“ ReferenceError:未定义”

时间:2019-04-18 11:41:40

标签: javascript html

ReferenceError尽管定义了函数。

尝试重命名

<body>
   <script>
    function yaSuggestion() {
      console.log("hello");
      var input = document.getElementById("yaIsInput");
      var request = new XMLHttpRequest();

      request.open("GET", "http://localhost:3000/issues/get");
      request.addEventListener('load', function (event) {
        if (request.status >= 200 && request.status < 300) {
          //console.log(request.responseText);
        } else {
          //console.warn(request.statusText, request.responseText);
        }
      });
      request.send();
      request.onreadystatechange = processRequest();
      function processRequest() {
        if (request.readyState == 4 && request.status == 200) {
          console.log(request.responseText);
          var res = JSON.parse(request.responseText);
          var suggestions = [];
          for (int i = 0; i < res.length; i++) {
            suggestions.push(res[i].value);
          };
          console.log(suggestions);
        }
      }
    }
  </script>
<input type="text" name="yaIsInput" class="form-control" id="yaIsInput" onchange="yaSuggestion()">
</body>

预期:控制台输出“ hello” 实际:ReferenceError:未定义yaSuggestion [了解更多] ya_create:1:1

1 个答案:

答案 0 :(得分:0)

首先,您的脚本中还有一个结束符'}':

使用此

 function yaSuggestion() {
          console.log("hello");
          }

您必须在调用函数之前先导入脚本:

   <body>
      <script>
        function yaSuggestion() {
          console.log("hello");
          }
      </script>
      <input type="text" name="yaIsInput" class="form-control" id="yaIsInput" onchange="yaSuggestion()">
    </body>