javascript console.log 不适用于 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22288310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:51:33  来源:igfitidea点击:

console.log not working for jQuery?

javascriptjquerygoogle-chrome

提问by the tao

Here is my jQuery at the bottom of html page:

这是我在 html 页面底部的 jQuery:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript">
    $(document).ready(function () {
        $('#login').click(function() {
            var co_id = $('#companies').val();
            console.log(co_id); 
        });
    });
    </script>

When I type this jQuery into the console, then type co_idit works, but when I run this in browser and choose a company and click the correct button, and I would assume when I type co_idinto chrome browser console, it would show my option value? But it is coming back as:

当我在控制台中输入这个 jQuery 时,然后输入co_id它可以工作,但是当我在浏览器中运行它并选择一家公司并单击正确的按钮时,我会假设当我co_id在 chrome 浏览器控制台中输入时,它会显示我的选项值?但它又回来了:

>co_id
ReferenceError: co_id is not defined

Here is html:

这是html:

<select name="" id="companies">
    <option value="--">--</option>
    <option value="1">Company</option>
    <option value="2">Company2</option>
    <option value="3">Company3</option>
    <option value="68">Company4</option>
    <option value="69">Company5</option>
    <option value="70">Company6</option>
</select><input id="login" type="submit" value="Login">

回答by ???? ??????

Don't write code if you set the src attribute.

如果您设置了 src 属性,请不要编写代码。

 <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript">
</script>
<script type="text/javascript">
$(document).ready(function () {
    $('#login').click(function() {
        var co_id = $('#companies').val();
        console.log(co_id);   
    });
});
</script>

回答by Rouven We?ling

Your problem is, that the Chrome console is always running in the global scope but co_idis scoped to the closure.

您的问题是,Chrome 控制台始终在全局范围内运行,但co_id仅限于闭包。

To inspect the values you have 3 options:

要检查值,您有 3 个选项:

  1. Declare co_id to be in the global scope. To do this simply remove the varin front of the variable name and add var co_idin the global scope. I don't recommend this, inevitably you'll forget to make the scope local again and you'll end up with a lot of global variables. They can be a source of a lot of fun bugs.

  2. Change the console.log()to actually output the variable content like this: console.log(co_id);.

  3. The best solution: Set a break point in the function by clicking on the line number in the script pane.

  1. 声明 co_id 在全局范围内。为此,只需删除var变量名前面的 并添加var co_id到全局范围内。我不推荐这样做,不可避免地你会忘记再次将作用域设为本地,最终会得到很多全局变量。它们可能是许多有趣错误的来源。

  2. 更改console.log()实际输出变量的内容是这样的:console.log(co_id);

  3. 最佳解决方案:通过单击脚本窗格中的行号在函数中设置断点。