IE and FireFox使用JavaScript清空上传控件input type="file"

本文介绍了一种使用JavaScript清空HTML中文件输入控件值的方法,避免了整个表单的重置,提供了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 From: http://genghaixu.javaeye.com/blog/198242

  上传控件<input type="file">的值不能通过JavaScript来修改。有时候我们在上传控件中插入了值,就只能通过form的reset功能来清空了,这样就有些麻烦:form里面其他的值也被reset了。
既然可以使用form的reset清空,那就有办法了:新建一个临时form,然后将需要清空的上传控件移入其中,reset之后,再移回原来所在位置,最后删除创建的临时form。于是有了下面的代码:

  1. var Upload = { 
  2. clear: function(id){ 
  3.   var up = (typeof id=="string")?document.getElementById(id):id; 
  4.   if (typeof up != "object"return null
  5.   var tt = document.createElement("span"); 
  6.   tt.id = "__tt__"
  7.   up.parentNode.insertBefore(tt,up); 
  8.   var tf = document.createElement("form"); 
  9.   tf.appendChild(up); 
  10.   document.getElementsByTagName("body")[0].appendChild(tf); 
  11.   tf.reset(); 
  12.   tt.parentNode.insertBefore(up,tt); 
  13.   tt.parentNode.removeChild(tt); 
  14.   tt = null
  15.   tf.parentNode.removeChild(tf); 
  16. }, 
  17. clearForm: function(){ 
  18.   var inputs,frm; 
  19.   if (arguments.length == 0) 
  20.   { 
  21.    inputs = document.getElementsByTagName("input"); 
  22.   }else
  23.    frm = (typeof arguments[0] == "string")?document.getElementById(arguments[0]):arguments[0]; 
  24.    if (typeof frm != "object"return null
  25.    inputs = frm.getElementsByTagName("input"); 
  26.   } 
  27.   
  28.   var fs=[]; 
  29.   for ( var i=0; i<inputs.length; i++ ) 
  30.   { 
  31.    if (inputs[i].type == "file") fs[fs.length] = inputs[i]; 
  32.   } 
  33.   var tf = document.createElement("form"); 
  34.   for ( var i=0; i<fs.length; i++ ) 
  35.   { 
  36.    var tt = document.createElement("span"); 
  37.    tt.id = "__tt__" + i; 
  38.    fs[i].parentNode.insertBefore(tt, fs[i]); 
  39.    tf.appendChild(fs[i]); 
  40.   } 
  41.   document.getElementsByTagName("body")[0].appendChild(tf); 
  42.   tf.reset(); 
  43.   for ( var i=0; i<fs.length; i++) 
  44.   { 
  45.    var tt = document.getElementById("__tt__" + i); 
  46.    tt.parentNode.insertBefore(fs[i],tt); 
  47.    tt.parentNode.removeChild(tt); 
  48.   } 
  49.   tf.parentNode.removeChild(tf); 
【全文】

    上传控件<input type="file">的值不能通过JavaScript来修改。有时候我们在上传控件中插入了值,就只能通过form的reset功能来清空了,这样就有些麻烦:form里面其他的值也被reset了。
既然可以使用form的reset清空,那就有办法了:新建一个临时form,然后将需要清空的上传控件移入其中,reset之后,再移回原来所在位置,最后删除创建的临时form。于是有了下面的代码:



  1. var Upload = { 
  2. clear: function(id){ 
  3.   var up = (typeof id=="string")?document.getElementById(id):id; 
  4.   if (typeof up != "object"return null
  5.   var tt = document.createElement("span"); 
  6.   tt.id = "__tt__"
  7.   up.parentNode.insertBefore(tt,up); 
  8.   var tf = document.createElement("form"); 
  9.   tf.appendChild(up); 
  10.   document.getElementsByTagName("body")[0].appendChild(tf); 
  11.   tf.reset(); 
  12.   tt.parentNode.insertBefore(up,tt); 
  13.   tt.parentNode.removeChild(tt); 
  14.   tt = null
  15.   tf.parentNode.removeChild(tf); 
  16. }, 
  17. clearForm: function(){ 
  18.   var inputs,frm; 
  19.   if (arguments.length == 0) 
  20.   { 
  21.    inputs = document.getElementsByTagName("input"); 
  22.   }else
  23.    frm = (typeof arguments[0] == "string")?document.getElementById(arguments[0]):arguments[0]; 
  24.    if (typeof frm != "object"return null
  25.    inputs = frm.getElementsByTagName("input"); 
  26.   } 
  27.   
  28.   var fs=[]; 
  29.   for ( var i=0; i<inputs.length; i++ ) 
  30.   { 
  31.    if (inputs[i].type == "file") fs[fs.length] = inputs[i]; 
  32.   } 
  33.   var tf = document.createElement("form"); 
  34.   for ( var i=0; i<fs.length; i++ ) 
  35.   { 
  36.    var tt = document.createElement("span"); 
  37.    tt.id = "__tt__" + i; 
  38.    fs[i].parentNode.insertBefore(tt, fs[i]); 
  39.    tf.appendChild(fs[i]); 
  40.   } 
  41.   document.getElementsByTagName("body")[0].appendChild(tf); 
  42.   tf.reset(); 
  43.   for ( var i=0; i<fs.length; i++) 
  44.   { 
  45.    var tt = document.getElementById("__tt__" + i); 
  46.    tt.parentNode.insertBefore(fs[i],tt); 
  47.    tt.parentNode.removeChild(tt); 
  48.   } 
  49.   tf.parentNode.removeChild(tf); 
下面是测试代码: 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <meta name="author" content="" /> 
  6. <meta name="keywords" content="" /> 
  7. <meta name="description" content="" /> 
  8. <title>Upload.clear</title> 
  9. <style type="text/css"
  10. </style> 
  11. <script type="text/javascript"
  12. var Upload = { 
  13. clear: function(id){ 
  14.   var up = (typeof id=="string")?document.getElementById(id):id; 
  15.   if (typeof up != "object"return null
  16.   var tt = document.createElement("span"); 
  17.   tt.id = "__tt__"
  18.   up.parentNode.insertBefore(tt,up); 
  19.   var tf = document.createElement("form"); 
  20.   tf.appendChild(up); 
  21.   document.getElementsByTagName("body")[0].appendChild(tf); 
  22.   tf.reset(); 
  23.   tt.parentNode.insertBefore(up,tt); 
  24.   tt.parentNode.removeChild(tt); 
  25.   tt = null
  26.   tf.parentNode.removeChild(tf); 
  27. }, 
  28. clearForm: function(){ 
  29.   var inputs,frm; 
  30.   if (arguments.length == 0) 
  31.   { 
  32.    inputs = document.getElementsByTagName("input"); 
  33.   }else
  34.    frm = (typeof arguments[0] == "string")?document.getElementById(arguments[0]):arguments[0]; 
  35.    if (typeof frm != "object"return null
  36.    inputs = frm.getElementsByTagName("input"); 
  37.   } 
  38.   
  39.   var fs=[]; 
  40.   for ( var i=0; i<inputs.length; i++ ) 
  41.   { 
  42.    if (inputs[i].type == "file") fs[fs.length] = inputs[i]; 
  43.   } 
  44.   var tf = document.createElement("form"); 
  45.   for ( var i=0; i<fs.length; i++ ) 
  46.   { 
  47.    var tt = document.createElement("span"); 
  48.    tt.id = "__tt__" + i; 
  49.    fs[i].parentNode.insertBefore(tt, fs[i]); 
  50.    tf.appendChild(fs[i]); 
  51.   } 
  52.   document.getElementsByTagName("body")[0].appendChild(tf); 
  53.   tf.reset(); 
  54.   for ( var i=0; i<fs.length; i++) 
  55.   { 
  56.    var tt = document.getElementById("__tt__" + i); 
  57.    tt.parentNode.insertBefore(fs[i],tt); 
  58.    tt.parentNode.removeChild(tt); 
  59.   } 
  60.   tf.parentNode.removeChild(tf); 
  61. </script> 
  62. </head> 
  63. <body> 
  64. <form name="f" id="f" method="post"
  65. <input type="file" name="f1" id="f1" /> 
  66. <input type="button" value="clear" onclick="Upload.clear('f1')" /><br /> 
  67. <input type="file" name="f2" id="f2" /><br /> 
  68. <input type="file" name="f3" id="f3" /><br /> 
  69. <input type="file" name="f4" id="f4" /><br /> 
  70. <input type="file" name="f5" id="f5" /><br /> 
  71. <input type="file" name="f6" id="f6" /><br /> 
  72. <input type="file" name="f7" id="f7" /><br /> 
  73. <input type="file" name="f8" id="f8" /><br /> 
  74. <input type="button" value="clearAll" onclick="Upload.clearForm()" /><br /> 
  75. <input type="submit" value="submit" /><input type="reset" value="reset" /> 
  76. </form> 
  77. <form name="form" id="form" method="post"
  78. <input type="file" name="f9" id="f9" /><br /> 
  79. <input type="file" name="f10" id="f10" /><br /> 
  80. <input type="button" value="clearThisForm" onclick="Upload.clearForm('form')" /> 
  81. </form> 
  82. </body> 
  83. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值