[英]jQuery document.ready does not fire, no jQuery works
I've looked at a few other similar questions here and tried the following: 我在这里查看了其他一些类似的问题,并尝试了以下操作:
I am trying to access data from a SQLite3 database (which works fine), in an html file. 我正在尝试从html文件中的SQLite3数据库(工作正常)访问数据。
<script src="jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(){console.log("HELLO");}
</script>
I do not get the console message, no matter where I place it! 无论放置在哪里,我都不会收到控制台消息! It seems none of my jQuery is working. 看来我的jQuery都不起作用。
Here's where I am trying to access the data: 这是我尝试访问数据的地方:
<div class="panel-body">
The website you're most active on:
<select onchange="dropdown(this)">
<option value='PastHour'>Past Hour</option>
<option value='Past2Hours'>Past 2 Hours</option>
<option value='PastDay'>Past 24 Hours</option>
</select><br><br>
<script type="text/javascript">
if()
<b>Most Frequently Visited:</b><div id="most-freq"></div>
<b>Second Most Frequently Visited:</b><div id="most-freq-2"></div>
</div>
function dropdown(that) {
const requestURL = 'data/' + that.value;
jQuery.ajax ({
url: requestURL,
type: 'GET',
dataType: 'json',
success: (data) => {
console.log('data:', data);
console.log(data.Most);
console.log(data.SecMost);
jQuery('#most-freq').html(data.Most);
jQuery('#most-freq-2').html(data.SecMost);
}
})
}
</script>
I can access localhost/data and get the data I want to see, so I believe the app.get functions in my .js file are okay. 我可以访问localhost / data并获取想要查看的数据,因此我相信.js文件中的app.get函数是可以的。
In addition to the data not being displayed or printed to console, I get the error message that dropdown is not defined. 除了未显示数据或未将其打印到控制台之外,我还收到错误消息,即未定义下拉列表。 I think this is because of the scope, how could I change it? 我认为这是因为范围,我该如何更改?
Your code should be: 您的代码应为:
jQuery(document).ready(function () {console.log("HELLO");});
instead of: 代替:
jQuery(document).ready(){console.log("HELLO");}
It does not throws any error log because there is no error. 它不会引发任何错误日志,因为没有错误。 Using the correct indentation it is easy to see the difference: 使用正确的缩进,很容易看到差异:
Correct behavior: 正确的行为:
jQuery(document).ready(function () {
console.log("HELLO");
});
Wrong behavior: 错误的行为:
jQuery(document).ready();
{
console.log("HELLO");
}
From JQuery ready's documentation link 从JQuery ready的文档链接
JQuery has three way to execute javascript code when the DOM is fully loader. 当DOM完全加载时,JQuery有三种执行javascript代码的方法。 These are: 这些是:
Example: 例:
$(function(){ do something ... });
or 要么
$(document).ready(function(){ do something .... });
Your Code function() is required. 您的代码 function()是必需的。 It specifies the function to run after the document is loaded 它指定了在文档加载后要运行的功能
jQuery(document).ready(){console.log("HELLO");}
If you're still having trouble try this: 如果您仍然遇到问题,请尝试以下操作:
Ref: Code from https://learn.jquery.com/using-jquery-core/document-ready/ 参考:来自 https://learn.jquery.com/using-jquery-core/document-ready/的 代码
$( document ).ready(function() {
console.log( "ready!" );
});
jQuery don't is for debugging purposes. jQuery并非用于调试目的。 You can instance the console when something happens. 您可以在发生某些情况时实例化控制台。
$('#buttonTest').click(function() {
console.log('you click on buttonTest');
// do something
});
For some reasons, the console object could be unavailable. 由于某些原因,控制台对象可能不可用。 Then you could check if it is - this is useful as you don't have to remove your debugging code when you deploy to production: 然后,您可以检查它是否有用-这很有用,因为在部署到生产环境时不必删除调试代码:
if (window.console && window.console.log) {
// console is available
}
and i suggest use the val with this method: 我建议使用val与这种方法:
<select id="mySelect" onchange="myFunction($(this).val());">
<option value="testVaue">Ford F150<option>
</select>
function myfunction(mySelect){alert("my val from select: "+mySelect );}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.