javascript - Javascript 参考错误
问题描述
我正在尝试在 keydown 上调用 javascript 函数,但出现错误:
/* Calling the javascript file */
$(document).ready(function() {
item_set();
});
/* custom.js */
function item_set(){
$(document).ready(function() {
function check(idname) {
// Allow: backspace, delete, tab, escape, enter and .
var e=document.getElementById(idname);
if (jQuery.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A, Command+A
(e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
};
};
<input type="text" name="item_cost" id="item_cost" class="form-control" onkeydown="check(item_cost)" />
控制台中的错误是:
ReferenceError:检查未定义
那有什么问题?
解决方案
我认为您的问题是您需要在调用文件中引用自定义脚本。
<script src="custom.js"></script>
<script>
$(document).ready(function() {
item_set(); //now you have access to this function
});
</script>
旁注:我喜欢把我所有的脚本放在末尾<body>
,这样你就不必费心了$(document).ready()
...
<body>
...
html
and
stuff
...
<script src="custom.js"></script>
<script>
item_set();
</script>
</body>
推荐阅读
- c# - NFC MIFARE Classic 1K 无法读取或写入
- sql-server - 如何用动态列替换 T-SQL PIVOT 中的 NULL 值
- android - 如何在横向使用 ViewPager2 进行轮播布局
- reactjs - 反应钩子关闭问题
- data-visualization - 二次排序(单独)
- spring-boot - 为特定请求暂停骆驼路线
- server - fs mksdirsync 在本地主机中创建文件夹但在服务器中不起作用
- ruby-on-rails - 如何在rails的.erb文件中添加符号(:)
- nao-robot - 如何控制 Pepper QiSDK 上的 LED?
- java - IntelliJ IDEA 中当前运行目录的问题