gwt - how can i trigger clickEvent on FileUpload widget?

萝らか妹 提交于 2020-01-21 07:51:08

问题


I' m trying to create a simple Button and when it's clicked I want to trigger a hidden FileUpload widget which is inside a FormPanel. What I have done until now is two things:

  1. I have created a native javascript function that clicks the FileUpload widget's element when simple button is clicked. This works fine in Firefox but it doesn't work in Chrome.
  2. Also, I have created this: NativeEvent nevent = Document.get().createFocusEvent();//I have tried and createclickEvent() too.
    DomEvent.fireNativeEvent(nevent, fileUploadWidget); This doesn't work at all.

Can anyone help me please??? I have searched a lot but I find nothing working.


回答1:


myFileUpload.getElement().<InputElement>cast().click()

is what you're looking for.

For it to work in WebKit-based browsers (Chrome, Safari), the FileUpload has to be "moved out of view" but not hidden (as in setVisible(false), which sets the CSS display property to none), i.e. something like (in CSS): position:absolute; top: -1000px; left: -1000px;




回答2:


I believe Thomas Broyer's answer is out of date now. I have just successfully used myFileUpload.click() with no problems.

I also used setVisible(false) on the the FileUpload field and it works in Chrome. Haven't tested in Safari but it would seem those issues have been resolved now.




回答3:


You can also use JSNI like this:

private native void triggerClick(Element e) /*-{
    e.click();
}-*/;

Then call

triggerClick(fileInput.getElement());


来源:https://stackoverflow.com/questions/5868957/gwt-how-can-i-trigger-clickevent-on-fileupload-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!