[英]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/export
的addEventListener
,最后它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.