当前位置: 代码迷 >> Web前端 >> clear file value (扫除文件框中的值)
  详细解决方案

clear file value (扫除文件框中的值)

热度:636   发布时间:2012-10-31 14:37:31.0
clear file value (清除文件框中的值)
1 js代码

<SCRIPT LANGUAGE="JavaScript">
  <!--
	var Upload = { 
clear: function(id){ 
  var up = (typeof id=="string")?document.getElementById(id):id; 
  if (typeof up != "object") return null; 
  var tt = document.createElement("span"); 
  tt.id = "__tt__"; 
  up.parentNode.insertBefore(tt,up); 
  var tf = document.createElement("form"); 
  tf.appendChild(up); 
  document.getElementsByTagName("body")[0].appendChild(tf); 
  tf.reset(); 
  tt.parentNode.insertBefore(up,tt); 
  tt.parentNode.removeChild(tt); 
  tt = null; 
  tf.parentNode.removeChild(tf); 
}, 

clearForm: function(){ 
  var inputs,frm; 
  if (arguments.length == 0) 
  { 
   inputs = document.getElementsByTagName("input"); 
  }else{ 
   frm = (typeof arguments[0] == "string")?document.getElementById(arguments[0]):arguments[0]; 
   if (typeof frm != "object") return null; 
   inputs = frm.getElementsByTagName("input"); 
  } 
  
  var fs=[]; 
  for ( var i=0; i<inputs.length; i++ ) 
  { 
   if (inputs[i].type == "file") fs[fs.length] = inputs[i]; 
  } 

  var tf = document.createElement("form"); 
  for ( var i=0; i<fs.length; i++ ) 
  { 
   var tt = document.createElement("span"); 
   tt.id = "__tt__" + i; 
   fs[i].parentNode.insertBefore(tt, fs[i]); 
   tf.appendChild(fs[i]); 
  } 
  document.getElementsByTagName("body")[0].appendChild(tf); 
  tf.reset(); 
  for ( var i=0; i<fs.length; i++) 
  { 
   var tt = document.getElementById("__tt__" + i); 
   tt.parentNode.insertBefore(fs[i],tt); 
   tt.parentNode.removeChild(tt); 
  } 
  tf.parentNode.removeChild(tf); 
} 
} 
//-->
</SCRIPT>


2 html调用示例
<form name="f" id="f" method="post"> 
<input type="file" name="f1" id="f1" /> 
<input type="button" value="clear" onclick="Upload.clear('f1')" /><br /> 
<input type="file" name="f2" id="f2" /><br /> 
<input type="file" name="f3" id="f3" /><br /> 
<input type="file" name="f4" id="f4" /><br /> 
<input type="file" name="f5" id="f5" /><br /> 
<input type="file" name="f6" id="f6" /><br /> 
<input type="file" name="f7" id="f7" /><br /> 
<input type="file" name="f8" id="f8" /><br /> 
<input type="button" value="clearAll" onclick="Upload.clearForm()" /><br /> 
<input type="submit" value="submit" /><input type="reset" value="reset" /> 
</form> 
<form name="form" id="form" method="post"> 
<input type="file" name="f9" id="f9" /><br /> 
<input type="file" name="f10" id="f10" /><br /> 
<input type="button" value="clearThisForm" 
onclick="Upload.clearForm('form')" /> 
</form> 
  相关解决方案