使用 jQuery 清除文件输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7769489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:15:33  来源:igfitidea点击:

clear file input field using jQuery

jqueryformsreset

提问by Ibrahim Azhar Armar

Possible Duplicate:
Blank out a form with jQuery

可能的重复:
使用 jQuery 清除表单

I am using a jQuery function to clear all my form values.

我正在使用 jQuery 函数来清除我所有的表单值。

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

the problem with above function is it does not reset the file input field. is there anyway i could extend the function and make it to work with clearing file input field as well.

上述功能的问题是它不会重置文件输入字段。无论如何我可以扩展该功能并使其也可以用于清除文件输入字段。

here is file input field HTML code i am using.

这是我正在使用的文件输入字段 HTML 代码。

<section><label for="productimg">Picture<br></label>
    <div><input type="file" id="productimg" name="productimg"></div>
</section>

thank you..

谢谢你..

回答by Jayendra

Try using the form reset, to get back to the initial values -

尝试使用表单重置,回到初始值 -

$('#form_id').each(function(){
    this.reset();
});

Demo - http://jsfiddle.net/hPytd/& http://jsfiddle.net/hPytd/1/

演示 - http://jsfiddle.net/hPytd/& http://jsfiddle.net/hPytd/1/

回答by Sjoerd

You can call the native reset() method on the form element:

你可以在表单元素上调用原生的 reset() 方法:

$('#myform')[0].reset();

回答by Jayantha Lal Sirisena

You can replace the file input with html to do this,

你可以用 html 替换文件输入来做到这一点,

 var id=$(this).attr('id');
    $(this).replaceWith("<input type='file' id='"+id+"' />");

回答by Allende

Have you ever try adding the type file to the validation,like this:

您是否尝试过将类型文件添加到验证中,如下所示:

if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'file')

if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'file')

回答by RobB

Depending on the full scope there are multiple methods to accomplish your desired result and they are much easier than you probably think:

根据整个范围,有多种方法可以实现您想要的结果,它们比您想象的要容易得多:

  1. Use <input type="reset" />to reset your form
  2. Build a .reset()function in jQuery:

    jQuery.fn.reset = function(){
      $(this).each (function() { this.reset(); }); 
    }
    
  1. 使用<input type="reset" />重置您的形式
  2. 在 jQuery 中构建一个.reset()函数:

    jQuery.fn.reset = function(){
      $(this).each (function() { this.reset(); }); 
    }
    

NOTE: Method 2 does workas it is a native JavaScript function.

注意:方法 2 确实有效,因为它是原生 JavaScript 函数。