简体   繁体   English

Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick

[英]Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick

I have a search form where I'm trying to have it output the results at the bottom of the page without reloading.我有一个搜索表单,我试图让它在页面底部输出结果而不重新加载。

<form action='' method='post'>
<div id="search-form">      
<input type="text" name="search" class="search-field" id="search" value="" />
<div class="submit-container">
<button type="button" value="" class="submit" id="searchbutton" onclick="searchoutput()" /></button>
            </div>
  </form>
  <br><br>
  <p>Type First Name</p>

I want the search results to show below when the button is clicked, using Ajax call to another script.我希望在单击按钮时搜索结果显示在下方,使用 Ajax 调用另一个脚本。 I keep getting an error: "Uncaught ReferenceError: searchoutput is not defined at HTMLButtonElement.onclick我不断收到错误消息:“未捕获的 ReferenceError:搜索输出未在 HTMLButtonElement.onclick 中定义

Here is my javascript (using jquery):这是我的 javascript(使用 jquery):

$( document ).ready(function() {
function searchoutput() {
     if($(".search-field").val().length > 5) { //only shows results when more than 5 characters have been entered
        var search = $(".search-field").val();
        var update = $(".result");
        var goal = 0;
        $.get("query-include.php" , {search: search, goal: goal})
        .done(function( data ) {
          update.empty();
          update.append(data);
          $(this).remove();
                                });
                                             };
                         } 
  $( ".search-field" ).keydown(function() {
      var update =$(".results");
      update.empty();
                                       });
                             });

I have checked other posts and spent a long time trying to get the solution on my own.我检查了其他帖子,并花了很长时间试图自己获得解决方案。 The odd thing is if I add an event listener for "keyup" in order to run the same function searchoutput as the user types:奇怪的是,如果我为“keyup”添加一个事件侦听器,以便运行与用户键入相同的函数 searchoutput:

var searchfield = document.getElementById("search");
searchfield.addEventListener("keyup", searchoutput);

Then I don't get the ReferenceError and the script runs fine.. I only get the function issue on button click.然后我没有得到 ReferenceError 并且脚本运行良好..我只在按钮单击时遇到功能问题。

It's a scoping issue - searchOutput is defined within the scope of the $(document).ready() function.这是一个范围问题 - searchOutput$(document).ready()函数的范围内定义。 What you could try is to declare a var for searchOutput before that code, then assign it to the function like this:您可以尝试在该代码之前为searchOutput声明一个 var,然后将其分配给函数,如下所示:

var searchOutput;
$( document ).ready(function() {
  searchOutput = function () {
   if($(".search-field").val().length > 5) {
   //etc.

I add an eventListener in the place of searchoutput();我在 searchoutput() 的位置添加了一个 eventListener;

 $( document ).ready(function() { $('.submit').addEventListener('click', function(){ if($(".search-field").val().length > 5) { //only shows results when more than 5 characters have been entered var search = $(".search-field").val(); var update = $(".result"); var goal = 0; $.get("query-include.php" , {search: search, goal: goal}) .done(function( data ) { update.empty(); update.append(data); $(this).remove(); }); } }); $( ".search-field" ).keydown(function() { var update =$(".results"); update.empty(); }); });

In my case, I was using modular code in my JS (ie import and export statements) so when I tried to use onclick , it wasn't calling my submit function.就我而言,我在我的 JS(即导入和导出语句)中使用模块化代码,所以当我尝试使用onclick ,它没有调用我的submit函数。 When I simply removed the import statement from my JS code and replaced dependent code with some default values, onclick called my function.当我简单地从我的 JS 代码中删除 import 语句并用一些默认值替换依赖代码时, onclick调用了我的函数。 Then I tried the addEventListener with import/export and finally it worked for me.然后我尝试了带import/exportaddEventListener ,最后它对我有用。

暂无
暂无

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

相关问题 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 得到此错误Uncaught ReferenceError:函数未在HTMLButtonElement.Onclick上定义? - Getting this error Uncaught ReferenceError: function is not defined at HTMLButtonElement.Onclick? ES6模块“Uncaught ReferenceError:函数未在HTMLButtonElement.onclick中定义” - ES6 module “Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick” 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:id 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义toggleFunction - Uncaught ReferenceError: toggleFunction is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM