现在的位置: 首页 > 综合 > 正文

js 获取、清空 input type=”file”的值

2013年09月03日 ⁄ 综合 ⁄ 共 1226字 ⁄ 字号 评论关闭

上传控件基础知识说明:

上传控件(<input type="file"/>)用于在客户端浏览并上传文件,用户选取的路径可以由value属性获取,但value属性是只读的,不能通过javascript来赋值,这就使得不能通过value=""语句来清空它。很容易理解为什么只读,如果可以随意赋值的话,那么用户只要打开你的网页,你就可以随心所欲的上传他电脑上的文件了。

 

js 获取<intput type=file />的值

<html>

 

 <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>
  <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:

 

<span   id=span1>  
  <input   name=ab   type=file>  
  </span>  
  <input   name=button1   type=button   value="按"   onclick=show()>  
  <script   language=javascript>  
  function   show()  
  {  
  document.getElementById("span1").innerHTML="<input   name=ab   type=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);
}

 

 

抱歉!评论已关闭.