我想清除我的表单中的文件输入。

我知道如何用同样的方法设置资源……但该方法不会擦除所选文件路径。

注意:我希望避免重新加载页面、重置表单或执行AJAX调用。

这可能吗?


当前回答

在我的世界里,一个简单干净的解决方案是,将文件输入文件设置为一个清晰的FileList。

编辑:正如SteJ所说,这不是与Safari合作。

由于我们不能直接创建FileList - I用户DataTransfer作为“hack”

@$input[0].files=new DataTransfer().files 

or

file_input_node.files=new DataTransfer().files 

其他回答

我一直在寻找简单而干净的方法来清除HTML文件输入,上面的答案是伟大的,但没有一个真正回答我在寻找什么,直到我在网络上遇到了一个简单而优雅的方法来做到这一点:

var $input = $("#control");

$input.replaceWith($input.val('').clone(true));

这一切都要归功于克里斯·科伊尔。

// Referneces var control = $("#control"), clearBn = $("#clear"); // Setup the clear functionality clearBn.on("click", function(){ control.replaceWith( control.val('').clone( true ) ); }); // Some bound handlers to preserve when cloning control.on({ change: function(){ console.log( "Changed" ) }, focus: function(){ console.log( "Focus" ) } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" id="control"> <br><br> <a href="#" id="clear">Clear</a>

解决方案

下面的代码是用jQuery编写的。它适用于所有浏览器,并允许保存事件和自定义属性。

var $el = $('#your-input-id');
$el.wrap('<form>').closest('form').get(0).reset();
$el.unwrap();

DEMO

有关代码和演示,请参阅jsFiddle。

链接

使用jQuery清除<input type='file' /> 如何用JavaScript重置文件输入

将值设为null或像这样的空字符串' ',以清除单个输入元素的值 您可以使用HTML按钮使用<input type= " reset " >属性轻松重置所有表单值。 HTMLFormElement.reset()方法可以恢复表单元素的默认值。此方法的作用与单击表单控件相同。

清除整个输入字段,而不必手动删除整个东西,或者如果在输入字段中已经有一个用户不想要的预先建议的输入。可能有很多情况。

您可以将此作为一个实际示例来尝试

const fileInputElement = document.getElementById('file-input'); const formElement = document.getElementById('input-form'); // Method-1: Clear files // fileInputElement.value = ''; // Method-2: Clear files // fileInputElement.value = null; // Method-3: Clear files - on event listener fileInputElement.addEventListener('change', (event)=>{ // If the file did not meet certain condition event.target.value = ''; }); // Method-4: Clear or reset whole form formElement.addEventListener('submit', (event)=>{ // If ajax call is failed to make request event.target.reset(); // or // formElement.reset() }); .form{ display: flex; flex-direction: column; width: 80%; margin-left: 10%; } #file-input, .form-control{ margin-top: 1rem; } .form-control{ padding: 1em 0; } #file-input{ background: #ccccff; } #file-input::-webkit-file-upload-button { background: #000033; color: white; padding: 1em; } .button-group{ display: flex; justify-content: start; } .btn{ background: #004d4d; color: white; width: 4rem !important; margin-right: 10px; } <form id='input-form' class='form' > <input type='file' id='file-input' /> <input type='text' class='form-control' id='text-input' placeholder='your name' /> <div class='button-group'> <button type='reset' class='form-control btn' id='reset-form'>Reset</button> <button type='submit' class='form-control btn' id='submit-form'>Submit</button> </div> </form>

document.getElementById('your_input_id').value=''

编辑: 这个选项不能在IE和opera中运行,但似乎可以在firefox、safari和chrome中运行。

我改变了类型文本和返回类型文件使用setAttribute

'<input file-model="thefilePic" style="width:95%;" type="file" name="file" id="filepicture" accept="image/jpeg" />'

'var input=document.querySelector('#filepicture');'

if(input != null)
{
    input.setAttribute("type", "text");
    input.setAttribute("type", "file");
}