简体   繁体   English

Javascript函数可在控制台中使用,但不能在代码中使用

[英]Javascript function works in console but not in code

I'm trying to install a jQuery plugin for input masking. 我正在尝试安装用于输入屏蔽的jQuery插件。 I've included the js file fine, and I try to initialize it per the docs: 我已经很好地包含了js文件,并尝试根据文档对其进行初始化:

<script src="autoNumeric.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    $('#balance-input').keyup(function(){
        $('.stripe-button').attr('data-amount', $('#balance-input').val());
    });
    $('#balance-input').autoNumeric('init');
  });
</script>

The first keyup function works fine and is unrelated to the second function. 第一个键入功能正常运行,并且与第二个功能无关。 In the console, I get an error that autoNumeric is undefined. 在控制台中,我收到一个错误,提示autoNumeric未定义。 Then I enter $('#balance-input').autoNumeric('init'); 然后我输入$('#balance-input')。autoNumeric('init'); into the console and it works without a problem. 进入控制台,它可以正常工作。 It's inside document.ready and the plugin is included prior to the document.ready. 它位于document.ready内部,并且在document.ready之前包含插件。 What to do? 该怎么办?

Even though you checked the load order, it really appears to be the problem. 即使您检查了装载顺序,也确实是问题所在。 Everything has been loaded by the time you get to using the console. 在您使用控制台时,所有内容都已加载。 But during the script it runs as soon as it can. 但是在脚本执行期间,它会尽快运行。 Document Ready waits for the DOM, but not necessarily all the scripts, depending on how they're loaded. 就绪文档等待DOM,但不一定要等待所有脚本,这取决于它们的加载方式。 Also, make sure only one core jQuery file is included (on some systems it might accidentally be included multiple times). 另外,请确保仅包含一个核心jQuery文件(在某些系统上,可能会意外地多次包含该文件)。

The next debugging step would be to set a breakpoint on the call to autoNumeric so that you can check what the state of the JavaScript load is at the time. 下一个调试步骤是在对autoNumeric的调用上设置一个断点,以便您可以检查当时JavaScript加载的状态。

You must add the script in different file(External javascript.js file), link it to your HTML and give 您必须将脚本添加到其他文件(外部javascript.js文件)中,并将其链接到HTML并给出
<body onload="functionname()"> . <body onload="functionname()"> What it will do is, run your script after the page loads. 它会做的是,在页面加载后运行脚本。
If you didn't understand refer this . 如果您不明白,请参考
Best Wishes..! 最好的祝愿..!

暂无
暂无

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

相关问题 Javascript代码可在控制台中使用,但不能在HTML中使用(单击功能) - Javascript code works in console but not HTML (Click Function) jQuery函数在控制台中有效,但在代码中无效 - jQuery function works in console but not in code JS 函数在控制台上有效,但在代码中无效 - JS function works on console, but not in code javascript 函数不执行,只能在控制台中工作 - javascript function does not execute, works only in console JavaScript函数仅在复制并粘贴到开发者控制台中时有效,而不能从代码中复制 - JavaScript function works only when copied and pasted in Developer console but not from the code Javascript 代码使用箭头函数而不是普通函数 - Javascript code works with arrow function and not with normal function 用于操作 Facebook 图像的 Javascript 代码在控制台中有效,但在扩展中无效 - Javascript Code for manipulating Facebook images works in console but not in extension Javascript代码在控制台中工作,但在加载时,因为未定义实际的用户脚本函数 - Javascript code works in console, but when loaded as actual userscript functions are not defined JavaScript代码仅在开发人员控制台中执行时有效 - JavaScript code only works when executed in the developer console JavaScript在Console中工作,而不在代码中使用new Date(parseInt( <datestring> )) - JavaScript works in Console not in code new Date(parseInt(<datestring>))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM