最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Simulate a click in <input type="file"> - Stack Overflow

programmeradmin0浏览0评论

Good day,

I'm trying to simulate a click on a browse file element but I couldn't make it work.

I found this: /

and tried and it works in 'button' and 'submit' types:

<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="submit" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

but when I tried to change it to 'file' type:

<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="file" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

It wouldn't work in Firefox and IE8. But the file browser window appears in Google Chrome.

Is there a problem with this approach? How could I make it work in FF and IE8?

Thank you very much!

Good day,

I'm trying to simulate a click on a browse file element but I couldn't make it work.

I found this: http://www.randomsnippets./2008/03/05/simulate-a-button-click-via-javascript/

and tried and it works in 'button' and 'submit' types:

<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="submit" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

but when I tried to change it to 'file' type:

<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="file" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

It wouldn't work in Firefox and IE8. But the file browser window appears in Google Chrome.

Is there a problem with this approach? How could I make it work in FF and IE8?

Thank you very much!

Share Improve this question asked Dec 15, 2010 at 15:31 srcastrosrcastro 3325 silver badges12 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

That's a security restriction to prevent malicious javascript from uploading files from a users puter without him wanting that or tricking him in doing so by opening a file selection dialog. You can't do it in javascript, and it's not a bug, it's a feature.

It also depends, why you need this? If this is for testing purposes you can use some existing frameworks, like FuncUnit:

http://www.beletsky/2010/12/functional-testing-by-javascript-with.html

发布评论

评论列表(0)

  1. 暂无评论