admin管理员组文章数量:1426220
I'm using Wicket 6.xx and I have a Wicket Form
that I use to upload some files. This is not a problem. The problem is that I need to run some JavaScript on the page after the form has been submitted in order to show a modal popup that is JavaScript based.
However, I can't do that because I don't have an AjaxRequestTarget
object, since a form submission is not an Ajax call. Or at least, I'm guessing that's the reason. I've tried the following, taken from another thread, but it doesn't work:
From<RequestInfo> uploadFrm = new Form<RequestInfo>("uploadFrm", getModel()) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
target.appendJavascript("..."); //NullPointerException: target is always null
}
};
As you can tell from the ment I put there, target
is always null
.
What is the proper way to run some JavaScript in a situation like this? Is there a proper way?
I'm using Wicket 6.xx and I have a Wicket Form
that I use to upload some files. This is not a problem. The problem is that I need to run some JavaScript on the page after the form has been submitted in order to show a modal popup that is JavaScript based.
However, I can't do that because I don't have an AjaxRequestTarget
object, since a form submission is not an Ajax call. Or at least, I'm guessing that's the reason. I've tried the following, taken from another thread, but it doesn't work:
From<RequestInfo> uploadFrm = new Form<RequestInfo>("uploadFrm", getModel()) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
target.appendJavascript("..."); //NullPointerException: target is always null
}
};
As you can tell from the ment I put there, target
is always null
.
What is the proper way to run some JavaScript in a situation like this? Is there a proper way?
Share Improve this question edited Nov 23, 2016 at 15:53 gus27 2,6561 gold badge23 silver badges25 bronze badges asked Nov 23, 2016 at 13:36 Master_TMaster_T 8,06116 gold badges91 silver badges171 bronze badges2 Answers
Reset to default 5If you don't want to go full Ajax as in m.bouali's answer, you can use a temporary behavior instead:
@Override
protected void onSubmit() {
add(new Behavior() {
protected boolean isTemporary(Component ponent) {
// this behavior will be removed after rendering
return true;
}
public void renderHead(Component ponent, IHeaderResponse response) {
response.render(JavaScritpHeaderItem.forScript("..."));
}
});
});
I think the best way to do that is to add an AjaxSubmitLink to your form so you can call javascript code inside the onSubmit method:
HTML code:
<form wicket:id="uploadFrm">
<input type="submit" wicket:id="ajaxSubmitLink" value="OK" />
</form>
JAVA code:
Form<RequestInfo> uploadFrm = new Form<RequestInfo>("uploadFrm", getModel());
AjaxSubmitLink ajaxSubmitLink = new AjaxSubmitLink("ajaxSubmitLink", uploadFrm) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
target.appendJavaScript("you javascript");
}
};
uploadFrm.add(ajaxSubmitLink);
本文标签: javaWicket run JavaScript when a Form is submittedStack Overflow
版权声明:本文标题:java - Wicket: run JavaScript when a Form is submitted - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745397265a2656873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论