简体   繁体   English

更改和onchange在我的计算机上不起作用。 是什么原因造成的?

[英]change and onchange don't work on my computer. what might be causing this?

The following code should pop up an alert box whenever I select a file or make a change to the textbox and click out of it. 每当我选择一个文件或对文本框进行更改并单击它时,以下代码应弹出一个警报框。

I believe the code SHOULD work, but it doesn't. 我相信该代码应该可以工作,但是不能。 Isolating the code snippets on JSFiddle and running them even works. 隔离JSFiddle上的代码段并运行它们甚至可以正常工作。

Is anything wrong with my code? 我的代码有什么问题吗? If not, could something on my computer be preventing this code from working? 如果不是,我的计算机上的某些东西会阻止此代码工作吗?

<html>

<head>

    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function() {
        document.getElementById("file").onchange(function() {
           alert($(this).val()); 
        });

        $('#text').change(function() {
           alert($(this).val()); 
        });
    });

    </script>
</head>


<body>
    <input id="file" type="file" name="file" />
    <input id="text" type="text" name="text" />
</body>

</html>

Try wrapping your function in 尝试将函数包装在

$(document).ready(function() {

});

jsfiddle does this for you (I think) jsfiddle为您做到这一点(我认为)

Alternatively use: 或者使用:

$(window).ready(function() {

});

if you want to ensure that the entire page has loaded before trying to run your code. 如果要确保在尝试运行代码之前已加载整个页面。

Further reading: http://docs.jquery.com/Tutorials:Introducing_ $(document).ready() 进一步阅读: http ://docs.jquery.com/Tutorials: Introducing_ $(document).ready()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM