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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745186451a4615659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信