我正在使用Wicket框架附加onchange事件。问题在于,在触发事件时,将继续返回旧值(这很有意义)。 请参见下面的代码。
我想做的就是在更改后获得选择/投递箱的“ new”值。
我该如何实现?
dropdown.add(new AjaxEventBehavior("onchange") {
/**
*
* @param target
*/
@Override
protected void onEvent(AjaxRequestTarget target) {
Component component = getComponent();
DropDownChoice dropdown = (DropDownChoice) component;
String value = dropdown.getValue();//This brings back the old value…
}
})
您将必须使用AjaxFormComponentUpdatingBehavior,否则将不会提交新值,然后调用getModelObject()获取新选择。
看看这个检票口的例子
Dropdown ajax example demo with code
注意方法:wantOnSelectionChangedNotifications
如果选择更改,是否使用javascript window.location调用此组件的onSelectionChanged事件处理程序。如果为true,则每次选择更改都会生成一次往返,从而导致更新模型(仅此组件的模型)并调用onSelectionChanged。默认情况下,此方法返回false。如果您希望改用Ajax,则让wantOnSelectionChangedNotifications()返回false并使用onchange事件将AjaxFormComponentUpdatingBehavior添加到组件中。
最好从基础模型中获取价值。同样,在访问变量“下拉列表”时也无需使用getComponent。
dropdown.add(new AjaxEventBehavior("onchange") {
/**
*
* @param target
*/
@Override
protected void onEvent(AjaxRequestTarget target) {
String value = dropdown.getModelObject();
}
})