Man*_*áoz 7 java wicket web-applications autocomplete
我正在使用Java和Wicket为webapp编写自动完成组件.
当用户选择自动完成列表的一个选项时,有没有办法处理onchange事件以运行某些代码?我尝试在AutoCompleteTextField中执行此操作:
setOutputMarkupId(true);
add(new AjaxEventBehavior("onchange") {
@Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println(getInput());
}
});
Run Code Online (Sandbox Code Playgroud)
但是getInput方法返回null.:(
有没有办法对onchange事件作出反应,并能够读取用户输入的内容?
谢谢你的时间和知识:)
onchange只有在焦点远离组件时才会触发该事件.(这是一个通用的浏览器/ javascript的东西.)
您需要将处理程序挂钩到onkeypress事件.
你需要的不是AjaxEventBehavior,但是AjaxFormComponentUpdatingBehavior:
add( new AjaxFormComponentUpdatingBehavior( "onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println( "Value: "+field.getValue() );
}
});
Run Code Online (Sandbox Code Playgroud)
虽然它也适用getInput(),但通常更高一级(适当地逃脱并由模型支持)getValue()更适合.