上传控件(<input type=”file”/>)用于在客户端浏览并上传文件,用户选取的路径可以由value属性获取,但value属性是只读的,不能通过 javascript来赋值,这就使得不能通过value=””语句来清空它。很容易理解为什么只读,如果可以随意赋值的话,那么用户只要打开你的网页, 你就可以随心所欲的上传他电脑上的文件了。
js 获取<intput type=file />的值
<html>
<head>
<title>清空input,type="file"的值</title>
<script language='javascript'>
function show(){
var p=document.getElementById("file1").value;
document.getElementById("s").innerHTML="<input id=pic type=image height=96 width=128 /> ";
document.getElementById("pic").src=p;
alert(p);
}
</script>
</head>
<body>
<input type="file" name="file1" id="file1" onpropertychange="show();" />
<span id="s"></span>
</body>
</html>
清空上传控件(<input type=”file”/>)的值的两种方法方法1:
<span id="span1">
<input name="ab" type="file">
</span>
<input name="button1" type="button" value="清空" onclick="clear()">
<script language=javascript>
function clear() {
document.getElementById("span1").innerHTML = "<input name='ab' type='file'>";
}
</script>
方法2:
<script language=javascript>
function clearFileInput(file){
var form=document.createElement('form');
document.body.appendChild(form);
//记住file在旧表单中的的位置
var pos=file.nextSibling;
form.appendChild(file);
form.reset();
pos.parentNode.insertBefore(file,pos);
document.body.removeChild(form);
}
</script>
上传文件时,选择了文件后想清空文件路径,搜索了一下,用两种方法解决
第三种:
obj.outerHTML=obj.outerHTML;//重新初始化了file的html
达维营-前端网