如何在Wicket中单击它后设置按钮enabled = false?

时间:2014-02-25 14:32:40

标签: button hyperlink wicket ajax-request wicket-6

我想要的是什么:

  • 如果我点击一个按钮,它应该不启用(不可点击) 在其他方法(点击)完成之前,只有AFTER 方法'结束。

这是按钮的代码:

@Override
protected void onInitialize() {
    add(createOkLink());
}

private IndicatingAjaxLink<Void> createOkLink() {
    final IndicatingAjaxLink<Void> ret = new IndicatingAjaxLink<Void>("okLink") {
        private static final long serialVersionUID = 1L;
        @Override
        public void onClick(AjaxRequestTarget target) {
            //when I click on it, this button (link) should not be enabled while the rest of the methods are not finished.
            method1(); //about 2-5 seconds running time
            method2(); //about 2-5 seconds running time
            method3(); //about 2-5 seconds running time
            feedback.success("success");
            target.add(feedback);
            //after every method has finished, the button should be clickable againg
        }
    };
    ret.setOutputMarkupId(true);
    return ret;
}

我希望有人可以帮助我!

我正在使用Wicket 6。

1 个答案:

答案 0 :(得分:11)

    final Form<?> form = new Form<>("form");
    form.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true);
    add(form
        .add(new FeedbackPanel("feedback"))
        .add(new AjaxButton("button") {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                info(System.currentTimeMillis());
                target.add(form);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            @Override
            protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
                super.updateAjaxAttributes(attributes);
                attributes.getAjaxCallListeners().add(new AjaxCallListener()
                    .onBefore("$('#" + getMarkupId() + "').prop('disabled',true);")
                    .onComplete("$('#" + getMarkupId() + "').prop('disabled',false);"));
            }
        }));