每当我选择文件或对文本框进行更改并单击它时,以下代码都会弹出警告框。
我相信代码应该工作,但事实并非如此。隔离JSFiddle上的代码片段并运行它们甚至可以正常工作。
我的代码有什么问题吗?如果没有,我的计算机上的某些东西是否会阻止此代码工作?
<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>
答案 0 :(得分:1)
尝试将您的功能包装在
中$(document).ready(function() {
});
jsfiddle为你做这件事(我认为)
或者使用:
$(window).ready(function() {
});
如果您想在尝试运行代码之前确保已加载整个页面。
进一步阅读: http://docs.jquery.com/Tutorials:Introducing_ $(文件)。就绪()