设置类型=“文件"的值.通过jQuery [英] Set value of type="file" via jquery

查看:50
本文介绍了设置类型=“文件"的值.通过jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:

以下答案反映了2009年旧版浏览器的状态.现在,您实际上可以在2017年使用JavaScript动态/以编程方式设置文件输入元素的值.

The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.

有关详细信息和演示,请参阅此问题的答案:
如何以编程方式设置文件输入值(即:拖放文件时?)

See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?

没有上载(浏览)文件,我想通过按钮单击来设置fileuploader的值.是否可能或我输入的方式错误?
范例html:

Without upload(browse) a file I want to set a value of fileuploader through button click.Is it possible or I am in wrong way?
sample html:

<input type="file" id="files" />
<input type="button" id="btnTest" value="Test"/>

和jquery:

 $("#btnTest").click(function() {
      alert('test');
     //$('#files').html('pp.img');
     //$('#files').val('pp.img');
     $('#files').text('pp.img')
});

但这对我不起作用.

推荐答案

好吧,我必须说,没有跨浏览器的方式可以做到这一点. 未选择文件"文本在小部件的实现定义部分中,并且完全是特定于浏览器的.并且如果有必要,您可以简单地使用CSS在value属性为空的情况下用一些文字覆盖文本.

well i must say that there is no cross-browser way to do this. The "no file selected" text is in the implementation-defined part of the widget, and it's totally browser specific. and if it is necessary You could simply use CSS to cover the text with something when the value attribute is empty.

这篇关于设置类型=“文件"的值.通过jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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