我正在尝试通过上传文件然后在
标记中显示文件名来做一些简单的事情,但它不起作用,我的代码是:
function smth () {
var x=document.getElementById("myfile");
var file=x.files[0];
var txt="";
txt=file.name;
document.getElementById("p1").innerHTML =txt;
}
<p id="p1"></p>
<input type="file" id="myfle">
<button onclick="javascript:smth()" value="clickme"></button>
最佳答案
这是错误的,因为输入的 id 值为 myfle 并且 documentgelementbyid 正在检索一个名为 #myfile 的不存在的元素。请参见下面的修复:
<p id="p1"></p>
<input type="file" id="myfile">
<button onclick="javascript:smth()" value="clickme"></button>
<script>
function smth () {
var x = document.getElementById("myfile");
var file = x.files[0];
var txt="";
txt=file.name;
document.getElementById("p1").innerHTML =txt;
}
</script>
关于javascript onclick 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38552861/