admin管理员组

文章数量:1314442

Ok so I have an input element of type file and id "test"

When I put in the address bar: javascript: document.getElementById("test").click() it brings up the open file dialog so the user can decide what to upload. However if this same exact line is inserted into the document or done in the console of chrome it does not bring up the open file dialog. In fact the console says that the click() function is undefined. Is there any way in chrome to do this?

Cause it seem to work fine for any of the other browsers

Ok so I have an input element of type file and id "test"

When I put in the address bar: javascript: document.getElementById("test").click() it brings up the open file dialog so the user can decide what to upload. However if this same exact line is inserted into the document or done in the console of chrome it does not bring up the open file dialog. In fact the console says that the click() function is undefined. Is there any way in chrome to do this?

Cause it seem to work fine for any of the other browsers

Share Improve this question asked Feb 2, 2011 at 18:11 AnonymousAnonymous 1111 gold badge1 silver badge3 bronze badges 1
  • what you're seeing as undefined is the return value of document.getElementById("test").click() and not the actual function being undefined. – Ravikiran Commented Feb 3, 2011 at 15:47
Add a ment  | 

2 Answers 2

Reset to default 1

You should wrap file-input element to other (ex.:div): HTTM:

<div>
<input type='file'>
<div>

CSS:

div{
height:1px;
overflow: hidden;
}

JS:

$('div input').click();

Good luck...

I had the same problem and managed to solve it(though I am using jQuery). I detailed the technique in another question

Jquery trigger file input

The idea was essentially to focus the file input before triggering the click programatically.

本文标签: javascriptInput File Click ChromeStack Overflow