handling to onchange event of AutoCompleteTextField in wicket

社会主义新天地 提交于 2019-12-23 08:52:58

问题


I'm writing an autocomplete component for a webapp using Java and Wicket.

Is there a way to handle the onchange event to run some code when the user selects one of the options of the autocomplete list? I tried doing this in the AutoCompleteTextField:

        setOutputMarkupId(true);
        add(new AjaxEventBehavior("onchange") {
            @Override
            protected void onEvent(AjaxRequestTarget target) {
                System.out.println(getInput());
            }
        });

But the getInput method returns null. :(
Is there a way to react to the onchange event and to be able to read what the user has entered?

Thanks for you time and knowledge :)


回答1:


The onchange event is only fired when the focus is moved away from the component. (This is a universal browser/javascript thing.)

You need to hook your handler to the onkeypress event instead.

What you need is not AjaxEventBehavior but AjaxFormComponentUpdatingBehavior:

    add( new AjaxFormComponentUpdatingBehavior( "onchange") {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            System.out.println( "Value: "+field.getValue() );

        }
    });

Although it works with getInput() too, but usually the somewhat higher level (properly escaped and backed by the model) getValue() is a better fit.



来源:https://stackoverflow.com/questions/5223610/handling-to-onchange-event-of-autocompletetextfield-in-wicket

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