永远不会调用updateAjaxAttributes

时间:2013-12-10 09:03:41

标签: events wicket listener keydown

在stackoverflow上我找到了Wicket的一些代码,这些代码应该响应用户在页面上的按键操作;我只做了一些调整。问题是当用户按下页面上的某些键时,永远不会调用updateAjaxAttributes方法和onEvent方法。代码如下:

    add(new AjaxEventBehavior("keydown")
    {
        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);

            IAjaxCallListener listener = new AjaxCallListener(){
                @Override
                public CharSequence getPrecondition(Component component) {
                    //this javascript code evaluates wether an ajaxcall is necessary.
                    //Here only by keyocdes for TAB and ENTER 
                    return  "var keycode = Wicket.Event.keyCode(attrs.event);" +
                            "if ((keycode == 9) || (keycode == 13))" +
                            "    return true;" +
                            "else" +
                            "    return false;";
                }
            };
            attributes.getAjaxCallListeners().add(listener);

            //Append the pressed keycode to the ajaxrequest 
            attributes.getDynamicExtraParameters()
                .add("var eventKeycode = Wicket.Event.keyCode(attrs.event);" +
                    "return {keycode: eventKeycode};");

            //whithout setting, no keyboard events will reach any inputfield
            attributes.setAllowDefault(true);
        }

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            //Extract the keycode parameter from RequestCycle
            final Request request = RequestCycle.get().getRequest();
            final String jsKeycode = request.getRequestParameters()
                            .getParameterValue("keycode").toString("");

            target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')");
        }
    });

有人能告诉我为什么从不调用这些方法并给我写一些工作代码? 提前致谢, 彼得

1 个答案:

答案 0 :(得分:1)

add(new AjaxEventBehavior(“onkeydown”)

需要“onkeydown”