本篇文章主要是对js获娶清空input type="file"的值的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
上传控件基础知识说明:
上传控件(<input type="file"/>)用于在客户端浏览并上传文件,用户选取的路径可以由value属性获取,但value属性是只读的,不能通过 javascript来赋值,这就使得不能通过value=""语句来清空它。很容易理解为什么只读,如果可以随意赋值的话,那么用户只要打开你的网页, 你就可以随心所欲的上传他电脑上的文件了。
js 获取<intput type=file />的值
代码如下:
<html>
<script language='javascript'>
functionshow(){
varp=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>
<title>MyHtml.html</title>
</head>
<body>
<input type="file" name="file1" id="file1" onpropertychange="show();" />
<span id="s"></span>
</body>
</html>
清空上传控件(<input type="file"/>)的值的两种方法
方法1:
代码如下:
<spanid=span1>
<inputname=abtype=file>
</span>
<inputname=button1type=buttonvalue="按"onclick=show()>
<scriptlanguage=javascript>
functionshow()
{
document.getElementById("span1").innerHTML="<inputname=abtype=file>";
}
</script>
方法2:
代码如下:
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);
}