如何用JavaScript或HTML将文件输入设置为无效? [英] How do you set file input to nothing with JavaScript or HTML?

查看:113
本文介绍了如何用JavaScript或HTML将文件输入设置为无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的网站上,我有一个用户可以上传文件的文件输入。它使用Ajax(不是真的)将文件发送到绑定到iframe的php文件。我的问题是Firefox自动填充文件输入元素。有没有一种方法可以让用户在不发送文件的情况下点击提交按钮?或者我将文件值设置为null,并检查在PHP文件中的?我的代码如下所示:

On the site I am developing, I have a file input that users can upload files from. It uses "Ajax" (not really) to send the file to a php file that is bound to an iframe. My question is that Firefox automatically fills in the file input element. Is there a way I can give users the option of clicking the submit button without sending the file? Or do I set the file value to null somehow and check for that in the php file? My code looks like this:

    Name: <input type='text' id='name'><br>
    Description: <textarea id='description' rows=10 columns=100></textarea><br>
    <form id="upload_form" method="post" enctype="multipart/form-data" action="uploadfile.php">
    <input type='hidden' value='' id='descriptionid' name='descriptionid'>
    <input type='file' name="file" id="file" size="27" value="">
    <input type='submit' value='Submit' onclick=';' id='submitPopup'>
    </form>

当按钮被点击时,它会运行JavaScript方法并获取名称和描述的值,并提交输入表单。我如何让用户选择上传文件,而不是通过浏览器自动填写文件?

When the button is clicked it runs a JavaScript method and gets the values of name and description and also submits the input form. How do I let users have the option of uploading a file, and not have it auto filled in by their browser?

推荐答案

作为一种安全措施,读取或设置文件的值输入字段是不允许的。但是,如果您调用 form.reset()来清除它。

As a security measure, reading or setting the value of a file input field is not allowed. However, if you call form.reset() that will clear it out for you.

你甚至可以循环通过所有其他输入,记住它们的值,重置表单然后重新填充其他输入,因此只清除文件 s。

You could even loop through all the other inputs, remember their value, reset the form and then refill the other inputs, so only the files are cleared.

这篇关于如何用JavaScript或HTML将文件输入设置为无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆