简体   繁体   English

使用 javascript/jquery 触发文件上传对话框

[英]trigger file upload dialog using javascript/jquery

instead of using <input type="file"> , is it possible to use <input type="text"> and then script it using javascript or jquery such that when the text box is clicked, the file upload dialogue shows up.....(and have it actually uploaded when it's submitted into a form)而不是使用<input type="file"> ,是否可以使用<input type="text">然后使用 javascript 或 jquery 编写脚本,以便在单击文本框时显示文件上传对话框.. ...(并在提交到表单时实际上传)

You mean something like this?你的意思是这样的?

http://jsfiddle.net/CSvjw/ http://jsfiddle.net/CSvjw/

$('input[type=text]').click(function() {
    $('input[type=file]').trigger('click');
});

$('input[type=file]').change(function() {
    $('input[type=text]').val($(this).val());
});

Note, though, that the value given by the file input is fake for security reasons.但请注意,出于安全原因,文件输入给出的值是假的。 If you want to just have the file name show up, you can cut out the slashes.如果只想显示文件名,可以剪掉斜杠。

Here's an example of how to do it using a string split and an array pop:下面是一个如何使用字符串拆分和数组弹出来执行此操作的示例:

http://jsfiddle.net/CSvjw/1/ http://jsfiddle.net/CSvjw/1/

$('input[type=text]').click(function() {
    $('input[type=file]').trigger('click');
});

$('input[type=file]').change(function() {
    var vals = $(this).val(),
        val = vals.length ? vals.split('\\').pop() : '';

    $('input[type=text]').val(val);
});

You can adjust this further to account for systems that use a forward slash as the directory separator.您可以进一步调整它以考虑使用正斜杠作为目录分隔符的系统。 It's also important to note that if you do this, you'll lose the functionality of many modern browsers where users can drag files from their computer directly onto a file input.同样重要的是要注意,如果您这样做,您将失去许多现代浏览器的功能,用户可以在这些浏览器中将文件从他们的计算机直接拖到文件输入中。 If I were you, I'd embrace that paradigm by styling the file input rather than trying to turn a text input into something that it is not.如果我是你,我会通过设计文件输入的样式来接受这种范式,而不是试图将文本输入变成它不是的东西。

And if the HTML code has identical multiple inputs like this one below:-如果 HTML 代码具有相同的多个输入,如下所示:-

<div class="item">
<input type="text" />
<input type="file" />
</div>
<div class="item">
<input type="text" />
<input type="file" />
</div>​​​​​​​​​​​​​​​​​​​​​

Expanding on @treeface's answer, the Jquery code (current version 1.8.0) would be:扩展@treeface 的答案,Jquery 代码(当前版本 1.8.0)将是:

$('input[type=text]').click(function() {
    $(this).parent(".item")
        .find('input[type=file]')
        .trigger('click');
});

$('input[type=file]').change(function() {
    $(this).parent(".item")
        .find('input[type=text]')
        .val($(this).val());
});​

Do take note between $parents() and $parent() in jQuery.请注意 jQuery 中的 $parents() 和 $parent() 之间的关系。 Try it out @ http://jsfiddle.net/afxDC/尝试一下@ http://jsfiddle.net/afxDC/

Dont use display:none or visibility:hidden initially in the css不要使用display:nonevisibility:hidden最初在 css 中

In Jquery :Jquery中:

$(document).ready(function() {
 $('#file').hide(); 
 $("#elementToBeClicked").click(function(){
   $('#file').click();
 });
});

I have the suspicion that due to security reasons you wont be able to do this.我怀疑由于安全原因,您将无法执行此操作。 I seem to remember a while back trying to set the value attribute of a file upload element which you can't do as you could pull specific files from a users computer without their consent.我似乎记得前一阵子试图设置文件上传元素的值属性,你不能这样做,因为你可以在未经用户同意的情况下从用户计算机中提取特定文件。 I'd imagine that this would extend to programmatically changing a text box to a file upload element as you could set the value of the text field to the file you wanted to add then change it's type to a upload element and submit the form.我想这会扩展到以编程方式将文本框更改为文件上传元素,因为您可以将文本字段的值设置为要添加的文件,然后将其类型更改为上传元素并提交表单。

It should be a simple enough thing to try although I'd think you're working within the limitations of Javascript and therefore if you can't do it in native JS you'd be unlikely to be able to use JQuery.尽管我认为您在 Javascript 的限制范围内工作,但尝试这应该是一件足够简单的事情,因此如果您不能在本机 JS 中做到这一点,您就不太可能使用 JQuery。

Hope this makes sense,希望这是有道理的,

JLove

i think you can bind the input text to a jquery/javascript function that will create an file input with code and the the user can now upload a file我认为您可以将输入文本绑定到 jquery/javascript 函数,该函数将使用代码创建文件输入,用户现在可以上传文件

<input type="text" onclick="upload"/>

function upload(){
    $('[input type="text"]').append('<input type="file"/>')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM